Solved

Need some help with CFont*

Posted on 2011-09-06
19
646 Views
Last Modified: 2012-05-12
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
Comment
Question by:tsp2002
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 8
19 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 36487238
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
 

Author Comment

by:tsp2002
ID: 36487606
Hi Andy,
will not stop at ->Create Font.....should that be a hint?
Let me know, Thanks
1.BMP
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 36487628
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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 

Author Comment

by:tsp2002
ID: 36487642
every point where I  ->Create Font is not hit and with a questionmark
0
 

Author Comment

by:tsp2002
ID: 36487666
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 36487679
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
 

Author Comment

by:tsp2002
ID: 36487685
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 36487691
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
 

Author Comment

by:tsp2002
ID: 36487723
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
 

Author Comment

by:tsp2002
ID: 36488214
This sounds like the cleanup routine is being called BEFORE any fonts are being created.
How is this possible?
0
 

Author Comment

by:tsp2002
ID: 36488240
Das Programm "[1616] Stockbruegger.exe: Systemeigen" wurde mit Code 0 (0x0) beendet.
is this any hint?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 36488311
>>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
 

Author Comment

by:tsp2002
ID: 36488315
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
 

Author Comment

by:tsp2002
ID: 36488355
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 36488690
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 36488701
To be honest I would only create and delete the fonts once, not each time it goes into the printing routine.
0
 

Author Comment

by:tsp2002
ID: 36489769
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
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 36490041
Yes - but also look very carefully at your sample code
0
 

Author Closing Comment

by:tsp2002
ID: 36494417
thank you
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question