Solved

Need some help with CFont*

Posted on 2011-09-06
19
637 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
  • 11
  • 8
19 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

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

Author Comment

by:tsp2002
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

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

Author Comment

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

Expert Comment

by:AndyAinscow
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Yes - but also look very carefully at your sample code
0
 

Author Closing Comment

by:tsp2002
Comment Utility
thank you
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Find Visual Studio Tools 2 77
sumHeights2  challenge 7 75
Python - ImportError: No module named 'urllib2' 2 98
wordlen challenge 3 46
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
This article will show, step by step, how to integrate R code into a R Sweave document
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now