CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: CFont::CreateFontPoint crashes

  1. #1
    Join Date
    May 2005
    Location
    Ellesmera
    Posts
    427

    Question CFont::CreatePointFont crashes

    I'm having a problem with the creation from CFont class

    here's how the function flow

    Code:
    OnDraw(CDC* pDC)
    {
    		 // calling a sub function
    		func1( pDc);
    }
     
    func1(CDC* dc)
    {
    	  CFont tempFont;
    	  LOGFONT tempLog;
     
    	 if( ! tempFont.CreatePointFont( 26, "Arial", dc ) )
    	 {
    		   // error handling
    		   return;
    	 }
     
    	 tempFont.GetLogFont(& tempLog);
    	 tempFont.DeleteObject();
    }

    on first try it works just fine, but during my regression testing suddenly the call to createFontPoint it suddenly failed.

    I could not point out what/where is the exact reason for the failure.
    Im sorry I can't post the whole code, just the point where the exact error occurs..

    anybody have any idea/experience with ??
    Last edited by ideru; June 4th, 2008 at 05:02 AM. Reason: wrong function name
    *** Con Tu Adios, Te Llevas, Mi Corazon***

    Traveling Encoder...

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,725

    Re: CFont::CreateFontPoint crashes

    What is CreateFontPoint? Where and how is it declared, defined, implemented?
    And how did it fail?
    And how does your "error handling" look like?
    Victor Nijegorodov

  3. #3
    Join Date
    May 2005
    Location
    Ellesmera
    Posts
    427

    Re: CFont::CreateFontPoint crashes

    oh im sorry I mean "CreatePointFont" function

    basically it returned a "FALSE" valued, meaning failed creation.

    the error handling basically returns a bool variable to the calling function which is "OnDraw".


    I tried digging deep into the type of error/exception and it seems that it is
    CResourceException that was the cause.

    Checking also the Task Manager, the memory use increase and never decrease.
    I think GDI is leaking obviously, check the code and GDI ( brush, pen , font ) were deleted
    after call to SelectObject().

    So Im currently confuse how to debug this, being a newbie with GDI.
    Are there any tool to check memory leak for GDI??
    *** Con Tu Adios, Te Llevas, Mi Corazon***

    Traveling Encoder...

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,725

    Re: CFont::CreateFontPoint crashes

    I don't see any leaking GDI objects in the code snippet you posted. I guess the cause of the problem is somewhere in the part of code you didn't show us.
    And BTW, why are you creating every time the same font? If this font is predefined you could create it only once (the first time) and same in the class member variable.
    Victor Nijegorodov

  5. #5
    Join Date
    May 2005
    Location
    Ellesmera
    Posts
    427

    Re: CFont::CreateFontPoint crashes

    Code:
    OnDraw(CDC* pDC)
    {
    		 // calling a sub function
    		func1( pDc);
    }
     
    func1(CDC* dc)
    {
    	  CFont tempFont;
    	  LOGFONT tempLog;
              HFONT hFont;
              HFONT hFontOrg;
     
    	 if( ! tempFont.CreatePointFont( 26, "Arial", dc ) )
    	 {
    		   // error handling
    		   return;
    	 }
     
    	 tempFont.GetLogFont(& tempLog);
    	 tempFont.DeleteObject();
    
    hFont = CreateFontIndirect(&m_Font);
    
             if( hFont )
             {
                 hFontOrg = (HFONT)SelectObject(hDC, hFont);
             }
    
             int bkMode = SetBkMode(hDC, TRANSPARENT);
             SetTextColor(hDC, m_clrText);
    
             // some point computation
             dc->LPtoDP(&rcObj);
             nSaveDC = SaveDC(hDC);
             
            // some more settings
            dc->DPtoLP(&rcObj);
            DrawTextEx();
     
             SetBkMode(hDC, bkMode);
            if( hFont )
            {
                 SelectObject(hDC, hFontOrg);
                 DeleteObject(hFont);
                 hFont = NULL;
            }
    
            RestoreDC(hDC, nSaveDC);
    
    }
    this is basically the code which process the CFont object.. Except for the continous creation of Font( planning to remove soon ) where did i go wrong??
    Last edited by ideru; June 4th, 2008 at 10:52 PM. Reason: &%'&
    *** Con Tu Adios, Te Llevas, Mi Corazon***

    Traveling Encoder...

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,725

    Re: CFont::CreateFontPoint crashes

    Quote Originally Posted by ideru
    this is basically the code which process the CFont object.. Except for the continous creation of Font( planning to remove soon ) where did i go wrong??
    Please, don't post any "basically" code snippets.
    Post only actual code snippets that can compile and can be tested.
    You code:
    a) is a wrong code
    b) doesn't compile
    c) is using uninitialized variables and trying to compare them to be non-zero
    Victor Nijegorodov

  7. #7
    Join Date
    Feb 2002
    Posts
    3,788

    Re: CFont::CreateFontPoint crashes

    Code:
    CFont tempFont;
    LOGFONT tempLog;
    HFONT hFont;
    HFONT hFontOrg;
    
    
    if( ! tempFont.CreatePointFont( 26, "Arial", dc ) )
    {
       // error handling
       return;
    }
    
    tempFont.GetLogFont(& tempLog);
    tempFont.DeleteObject();
    
     CreateFontIndirect(&m_Font);
    
     if( hFont )
     {
         hFontOrg = (HFONT)SelectObject(hDC, hFont);
     }
    what do you think the bolded lines will do while the code is being executed?

  8. #8
    Join Date
    May 2005
    Location
    Ellesmera
    Posts
    427

    Re: CFont::CreateFontPoint crashes

    @Alin, thanks for pointing it out.

    @VictorN, well its obviously not complete, and as I said in the first post I can't post the whole source code , I know am asking too much.

    Right now, since the code is so big we're trying to overhaul it and isolate the errors/problems.

    So thanks for the input ..
    *** Con Tu Adios, Te Llevas, Mi Corazon***

    Traveling Encoder...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)