[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 651
  • Last Modified:

Need some help with CFont*

Hello,
I need some help with my printing program.
In my h.file I have 7 pointer for my fonts
CFont* m_fontUeberschrift1;
CFont* m_fontUeberschrift2;
CFont* m_fontUeberschrift3;
CFont* m_fontUeberschrift4;
CFont* m_fontUeberschrift5;
CFont* m_fontUeberschrift6;
CFont* m_fontUeberschrift7;


When I declare a new pointer  (No.8) like
CFont* m_fontUeberschrift8; my program will crash. Please look at my stack.

With only 7 pointers everything runs fine. With the 8 pointers my program crash when I start.










here I create the fonts for my printing
void CAuftragsHistory_Lieferschein_Dialog::On_MY_BeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
      //Druckbereich in 0,1mm
      pDC->SetMapMode(MM_LOMETRIC);
    //--------------------------------------
      
    pInfo->SetMaxPage(1);
    //--------------------------------------
 
      //--------------- Neue Schrift erstellen ------------------
              m_fontUeberschrift1 = new CFont;
     m_fontUeberschrift1->CreateFont(-75,
                                         0,
                                       0,
                                         0,
                                         700,
                                         0,
                                            0,
                                         0,
                                         DEFAULT_CHARSET,
                                         OUT_CHARACTER_PRECIS,
                                         CLIP_CHARACTER_PRECIS,
                                         DEFAULT_QUALITY,
                                         DEFAULT_PITCH|FF_DONTCARE,
                                         "Arial");
    //----------------------------------------------------------
.
.
.
.

---------------------------------------------------------------------------------------------------------



here I print and use the different fonts
void CAuftragsHistory_Lieferschein_Dialog::On_MY_Print(CDC* pDC, CPrintInfo* pInfo)
{


      //Druckbereich in 0,1mm
      pDC->SetMapMode(MM_LOMETRIC);
    //--------------------------------------
      pInfo->SetMaxPage(1);
   //--------------------------------------

 

      CFont* pOldFont = pDC->SelectObject(m_fontUeberschrift1);
      //--------------------- Briefkopf -------------------------------
       pDC->TextOut(155,-180,"WILHELM STOCKBRÜGGER OHG");
.
.
.
.



---------------------------------------------------------------------------------------------------------



here I delete the fonts
void CAuftragsHistory_Lieferschein_Dialog::On_MY_EndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
      delete m_fontUeberschrift1;
       m_fontUeberschrift=NULL;

      delete m_fontUeberschrift2;
       m_fontUeberschrift1=NULL;
.
.
.
.

---------------------------------------------------------------------------------------------------------










When I create the new font here it works fine.

      void CAuftragsHistory_Lieferschein_Dialog::On_MY_Print(CDC* pDC, CPrintInfo* pInfo)
{


      //Druckbereich in 0,1mm
      pDC->SetMapMode(MM_LOMETRIC);
    //--------------------------------------
      pInfo->SetMaxPage(1);
   //--------------------------------------

 

      CFont* pOldFont = pDC->SelectObject(m_fontUeberschrift1);
      //--------------------- Briefkopf -------------------------------
       pDC->TextOut(155,-180,"WILHELM STOCKBRÜGGER OHG");
      
     pDC->SelectObject(m_fontUeberschrift1);
     pDC->TextOut(500,-265,"Stahl - Service - Center");


       CString strSchriftart2="Microsoft Sans Serif";
     
       CFont *fontPtr = new CFont();
       fontPtr->CreateFont(
       40,
        0,
        0,
        0,
       FW_NORMAL,
        0,
        0,
        0,
       DEFAULT_CHARSET,
       OUT_CHARACTER_PRECIS,
       CLIP_CHARACTER_PRECIS,
       DEFAULT_QUALITY,
       DEFAULT_PITCH|FF_DONTCARE,
       strSchriftart2);

 pDC->SelectObject(fontPtr);
 pDC->TextOut(1470,-570,"E-mail: my email");
 fontPtr->DeleteObject();
.
.
.
.
.
 

Can I use only a few pointer....like here 7. Or is there any other reason that my program will crash.
Please let me know.
500 points.
Best regards,
Thomas


1.BMP
0
tsp2002
Asked:
tsp2002
  • 11
  • 8
1 Solution
 
AndyAinscowCommented:
Just declaring the pointer should not give any problems.
I would guess you have a problem elsewhere - have you put a breakpoint in the code where the CFont is actually created ( ...=new CFont....) and seen if that line of code runs.  Also the same where it is deleted.
0
 
tsp2002Author Commented:
Hi Andy,
will not stop at ->Create Font.....should that be a hint?
Let me know, Thanks
1.BMP
0
 
AndyAinscowCommented:
Erm, that is the fifth font not the eigth as you said in the question.

Do you mean the breakpoint is never hit?  Or that it is hit and you can single step over it without problem?


With breakpoints you can find out which lines/blocks of codes are being run.  This helps you track down where the problem really is.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
tsp2002Author Commented:
every point where I  ->Create Font is not hit and with a questionmark
0
 
tsp2002Author Commented:
There is also a funny thing
When I go through my program with the breakpoint it will first stop at

->delete m_fontUeberschrift1;
 m_fontUeberschrift=NULL;
0
 
AndyAinscowCommented:
So the problem seems to be nothng to do with creating the font (don't assume, check).

Does it exit the routines where the cleanup is performed without crashing.

You could attempt to delete the same pointer twice for example eg.


delete m_fontUeberschrift7;
 m_fontUeberschrift7=NULL;
delete m_fontUeberschrift7;   <---  CRASH is very likely here
 m_fontUeberschrift8=NULL;
0
 
tsp2002Author Commented:
here is also my code how I call the print function.
Strange is that with a breakpoint it will first stop at delete m_fontUeberschrift1;




void CAuftragsHistory_Lieferschein_Dialog::Drucken_weiss(void)
{
        HANDLE hDevMode;    
      HANDLE hDevNames;      
 

    //ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
       if (GetPrinterDevice(_T("\\\\DSERVER\\Brother HL-7050 series"), &hDevNames, &hDevMode)) // See KB article Q166129
       {
          AfxGetApp()->SelectPrinter(hDevNames, hDevMode);
          CPrintDialog dlgPrint(FALSE,PD_PAGENUMS|PD_USEDEVMODECOPIESANDCOLLATE , this);
 
               //________________________ Papier Auswahl DEVMODE _______________________________
          if(AfxGetApp()->GetPrinterDeviceDefaults(&dlgPrint.m_pd))
          {               
                    LPDEVMODE dev=dlgPrint.GetDevMode();
                    //------------------------------------------------------------------------------
               // hier kann man alle Atribute von DEVMODE angeben (siehe Microsoft DEVMODE Blätter)
             // dev->dmOrientation=DMORIENT_LANDSCAPE; //Querformat
              //dev->dmOrientation=DMORIENT_PORTRAIT;  //Hochformat
                   // dev->dmPaperSize=DMPAPER_A4;
                   // dev->dmCopies=1;
                   // dev->dmDefaultSource=0;
                   // dev->dmPaperSize=DMPAPER_A5;
               // dev->dmPaperLength=1020;
               // dev->dmPaperWidth=1480;
              //  dev->dmDefaultSource=DMBIN_TRACTOR;
            //----------------------------------------
                     dev->dmOrientation=DMORIENT_PORTRAIT;
               dev->dmPaperSize=DMPAPER_A4;
               dev->dmCopies=1;
               dev->dmDefaultSource=0;
                      
                //Wichtig !!!dmFields setzen
              dev->dmFields |=DM_PAPERLENGTH|DM_PAPERWIDTH|DM_ORIENTATION|DM_DEFAULTSOURCE;
                    GlobalUnlock(dev);//muss hier stehen !!!!!!!! wichtig
              }
          //__________________________________________________________________________________

 
                  dlgPrint.m_pd.Flags &= ~PD_RETURNDC;   // Reset flag set by constructor.
                   //---------------------------------------------------------------------------------
                // Force the CPrintDialog to use our device mode & name.
              dlgPrint.m_pd.hDevMode  = CopyHandle(hDevMode);
              dlgPrint.m_pd.hDevNames = CopyHandle(hDevNames);
                    //---------------------------------------------------------------------------------





          //===============================================================================================
              //===============================================================================================
       //       if(dlgPrint.DoModal()==IDOK) //Ohne CPrintDialogBox
   // {
            
              hDevMode  = CopyHandle(dlgPrint.m_pd.hDevMode);  // Save new copies.
              hDevNames = CopyHandle(dlgPrint.m_pd.hDevNames); // Save new copies.

                     CDC dc;
                     HDC hDCPrint = dlgPrint.CreatePrinterDC();
                     //------------------
                     dc.Attach(hDCPrint);
                     //------------------
                     //dc.Attach(dlgPrint.GetPrinterDC());
                 dc.m_bPrinting=TRUE;
                                  
                    //------------------------------------
                      DOCINFO Print;
               memset( &Print, 0, sizeof(DOCINFO) );
               Print.cbSize = sizeof (DOCINFO);
               Print.lpszDocName = "Drucken im Dialog";
               Print.lpszOutput = (LPTSTR) NULL;
               Print.lpszDatatype = (LPTSTR) NULL;
               Print.fwType = 0;
                //-----------------------------------
                                      
                   BOOL bPrintingOK = dc.StartDoc(&Print);
                 CPrintInfo pInfo;
                 pInfo.m_rectDraw.SetRect(0,0,dc.GetDeviceCaps(HORZRES), dc.GetDeviceCaps(VERTRES));
                 
               //~~~~~~~~~~~~~~~~~~~~~~~~~
                 On_MY_BeginPrinting(&dc,&pInfo);  // call prepare brauch ich nicht
               //~~~~~~~~~~~~~~~~~~~~~~~~~


                     //------------------------------------------------------------------------------
                 for(UINT page=pInfo.GetMinPage();page<=pInfo.GetMaxPage() &&bPrintingOK ; page++)
               {
                   //begin a new page
                       dc.StartPage();
                   pInfo.m_nCurPage=page;
                   //~~~~~~~~~~~~~~~~~~~~~~~~~
                     On_MY_Print(&dc,&pInfo); // call Print your Page
                   //~~~~~~~~~~~~~~~~~~~~~~~~~
                     bPrintingOK=(dc.EndPage()>0);
                 }
               //-----------------------------------------------------------------------------
               
                        if(bPrintingOK)
                           {
                      dc.EndDoc();
                           }
                           else
                           {
                                 dc.AbortDoc();
                           }
                           
                   //~~~~~~~~~~~~~~~~~~~~~~~~~
                           On_MY_EndPrinting(&dc,&pInfo); // call your clean up here
                           //~~~~~~~~~~~~~~~~~~~~~~~~~
                           dc.DeleteDC();
//
 //  }// Ohne CPrintDialogBox

             //===============================================================================================
                 //===============================================================================================






              //===== sehr wichtig muss hier stehen ======
                  GlobalFree(dlgPrint.m_pd.hDevMode);
                  GlobalFree(dlgPrint.m_pd.hDevNames);

                  GlobalFree(hDevMode);
              GlobalFree(hDevNames);
                    //==========================================
       }
       //oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
                  
 
        
}
0
 
AndyAinscowCommented:
Soryy missed the first of the previous two comments.

This sounds like the cleanup routine is being called BEFORE any fonts are being created.  
0
 
tsp2002Author Commented:
You could attempt to delete the same pointer twice for example eg.


delete m_fontUeberschrift7;
 m_fontUeberschrift7=NULL;
delete m_fontUeberschrift7;   <---  CRASH is very likely here
 m_fontUeberschrift8=NULL;




no thats not the case.....strange is also that it will not stop at the first delete only at the second....and after that almost on every....but will also miss one
void CAuftragsHistory_Lieferschein_Dialog::On_MY_EndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
      delete m_fontUeberschrift1;
       m_fontUeberschrift=NULL;

      delete m_fontUeberschrift2;
       m_fontUeberschrift1=NULL;
.
.
.
.
0
 
tsp2002Author Commented:
This sounds like the cleanup routine is being called BEFORE any fonts are being created.
How is this possible?
0
 
tsp2002Author Commented:
Das Programm "[1616] Stockbruegger.exe: Systemeigen" wurde mit Code 0 (0x0) beendet.
is this any hint?
0
 
AndyAinscowCommented:
>>How is this possible?

You would have to single step through the code and look at the call stack.  It could be possible your routine to create the fonts is never called.


>>Das Programm...

No, that is just the exit code.  Not a great deal of help in determining what is wrong.
0
 
tsp2002Author Commented:
Andy,
that is strange, I did the following:
Projektmappe bereinigen and Projektmappe neuerstellen and now everything works...no crash...I can use font No 8
and it stops at every preakpoint from new CFont  to delete.
But I still get the questionmark at Create (see hardcopy that I posted earlier)
It runs without a crash. Do you have an explaination? Thank you


0
 
tsp2002Author Commented:
Andy, also would it be better to create the font in Print Function


  void CAuftragsHistory_Lieferschein_Dialog::On_MY_Print(CDC* pDC, CPrintInfo* pInfo)
{


      pDC->SetMapMode(MM_LOMETRIC);
    //--------------------------------------
      pInfo->SetMaxPage(1);
   //--------------------------------------
   CString strSchriftart2="Microsoft Sans Serif";
     
       CFont *fontPtr = new CFont();
       fontPtr->CreateFont(
       40,
        0,
        0,
        0,
       FW_NORMAL,
        0,
        0,
        0,
       DEFAULT_CHARSET,
       OUT_CHARACTER_PRECIS,
       CLIP_CHARACTER_PRECIS,
       DEFAULT_QUALITY,
       DEFAULT_PITCH|FF_DONTCARE,
       strSchriftart2);

 pDC->SelectObject(fontPtr);
 
 

 

         //--------------------- Briefkopf -------------------------------
       pDC->TextOut(155,-180,"WILHELM STOCKBRÜGGER OHG");
       
        pDC->TextOut(500,-265,"Stahl - Service - Center");

     //-------------------------------------------------------------------

fontPtr->DeleteObject();.



//now create a new font  
 fontPtr->CreateFont(
       80,
        0,
        0,
        0,
       FW_NORMAL,
        0,
        0,
        0,
       DEFAULT_CHARSET,
       OUT_CHARACTER_PRECIS,
       CLIP_CHARACTER_PRECIS,
       DEFAULT_QUALITY,
       DEFAULT_PITCH|FF_DONTCARE,
       strSchriftart2);

 pDC->SelectObject(fontPtr);
 
pDC->TextOut(1470,-570,"E-mail: my email");
fontPtr->DeleteObject();.



   
0
 
AndyAinscowCommented:
pDC->SelectObject(fontPtr);  
pDC->TextOut(1470,-570,"E-mail: my email");
fontPtr->DeleteObject();.


You ought to reselect the previous font into the DC before deleting the font

eg.

CFont pFontOld = pDC->SelectObject(fontPtr);  
pDC->TextOut(1470,-570,"E-mail: my email");
pDC->SelectObject(pFontOld);
fontPtr->DeleteObject();.
0
 
AndyAinscowCommented:
To be honest I would only create and delete the fonts once, not each time it goes into the printing routine.
0
 
tsp2002Author Commented:
So do you mean
create the fonts f.e. in OnIntitDialog()
and delete the fonts in the Destructor with
{
      delete m_fontUeberschrift1;
       m_fontUeberschrift=NULL;

      delete m_fontUeberschrift2;
       m_fontUeberschrift1=NULL;
.
.
.
.
}

0
 
AndyAinscowCommented:
Yes - but also look very carefully at your sample code
0
 
tsp2002Author Commented:
thank you
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 11
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now