CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2009
    Location
    Austria
    Posts
    4

    Angry Problems with CreatePen (WinGDI.h)

    Hi,

    First of all please don't get upset of me because my questions is not 100% on the right forum. I tried everywhere but I got no answer. I only expect some hints, some new ideas to try, maybe I will have luck.

    Ok, here is the deal. I've made a Win32 Application that is plotting some graph/plot. Things work nice and I was happy. I used this application to test the functions that are building up the plot. I want to use these function in my Photoshop plug-in (please read further... pleaseee). The plug-in is just a special dll. (Win32 Application and Plug-in are written in VS2008 - no MFC is used).

    In the plug-in (or just call it dll) all the basic functions like Rectangle(....), MoveToEx(...), LineTo(...), TextOut(...) work just fine. The problem is with CreatePen(...), CreateSolidBrush(...) and I think also with CreateFont(..). When one of these functions are called, the Photoshop (I tried with 2 different versions on 2 different laptops) is going crazy: all graphical stuffs on the screen are mixed up, my Windows Taskbar is up, parts from the left screen are right, it is like a puzzle game with my screen - I cannot do anything, I have to switch of the PC (from the button). This doesn't happen when I ran the win32 application in parallel with Photoshop.

    My guess is that these functions are accessing some resources that are already used by the main application (in this case Photoshop). Anyhow I'm going crazy, I'm losing days only trying to set a pen and a brush.

    If you have an idea, it doesn't matter how good or crazy it is, please drop a line. I will appreciate every hint.

    Thanks a lot,
    Paulie

  2. #2
    Join Date
    Jun 2007
    Location
    MA-USA
    Posts
    247

    Re: Problems with CreatePen (WinGDI.h)

    If i remember right, windows only have a few HDC to spread around.
    So if resources are used up (or you have leak) this may happen.
    Are you using GetDC and ReleaseDC before/after each drawing operation?
    And what is the CreatePen, CreateBrush etc. return value, is it valid?
    Also, who's HDC are you using to create this objects with?

  3. #3
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: Problems with CreatePen (WinGDI.h)

    Another thing to note, after you create a pen, brush, whatever, in order to use it you must select it into a DC. The object you created also used a GDI object, and as noted, there is a limited number of GDI objects avaialble.

    To solve this, after you create your pen (or brush, etc), select it into the DC, and use it, you must then deselect it from the DC, and then delete the object.

    You can see if this is a GDI object leak by using the task manager. Go to the Processes page, then use the View menu, Select columns, to display the hanles and GDI objects. If you see them constantly increasing, you have a resource leak - probably caused by not deleting your pens/brushes, etc.

    Hope that helps.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  4. #4
    Join Date
    Jun 2007
    Location
    MA-USA
    Posts
    247

    Re: Problems with CreatePen (WinGDI.h)

    I thought of a scenario that could trash the desktop...
    Code:
    HWND hwnd = GetBadHwndSomehow(); // returns hwnd = NULL
    HDC hdc = GetDC(hwnd); // returns HDC to desktop if HWND is NULL
    // ... do stuff with hdc ...

  5. #5
    Join Date
    Apr 2009
    Location
    Austria
    Posts
    4

    Resolved Re: Problems with CreatePen (WinGDI.h)

    Thank you guys,

    It seems that i forgot to delete the brush and the pen. After I used: DeleteObject() everything was ok.
    I still find it weird why it works without DeleteObject(hPen) in a Win32 application but not as a plug-in. Anyhow now I can go further with my work.

    Thanks again,
    Paulie

  6. #6
    Join Date
    Apr 2009
    Location
    Austria
    Posts
    4

    Unhappy Re: Problems with CreatePen (WinGDI.h)

    Hi,

    I used the DeleteObject() function to remove fonts, brush, pen and so on but I still have some problems. I still detect a small increasing of the GDI objects in Task Manager. It's not like before but if I use my application for a long time it is possible to get the same results.

    The purpose of all this is to plot a graph - it has only some basic functions. For this I wrote a class. Pens, fonts and brushes are declared only as member of the class and not as local variable (in functions). Below is the relevant code:

    in class:
    ----------------------------------------------------------------------------
    private:
    HWND hArea; // item bzw. object where to paint
    HDC hDC; // specify the window for painting
    PAINTSTRUCT Ps; // paint structure
    HPEN hPen; // handler for the pen
    HBRUSH hBrush; // handler for the brush
    RECT rArea; // painting area coordinates
    LOGFONT LogFont; // font type
    HFONT hFont; // font handler
    COLORREF aPlotColors[4]; // list with the colors


    void SimplePlot::Start()
    {
    hDC = BeginPaint(hArea, &(Ps));
    }

    void SimplePlot::Stop()
    {
    DeleteDC(hDC);
    DeleteObject(hPen);
    DeleteObject(hBrush);
    DeleteObject(hFont);
    EndPaint(hArea, &Ps);
    }

    In the other member functions of the class I use CreateBrush, CreatePen, CreatePen but all the results are assigned to the data member (hPen, hFont ..... )

    ------------------------------------------------------
    In main application:

    WM_PAINT:
    {
    myPaint.Start();
    ..... some code
    myPain.Stop();
    }

    With this code I can see a slight increasing of the GDI Objects after each WM_PAINT. Around 20 new objects every time.

    Paulie

  7. #7
    Join Date
    Apr 2009
    Posts
    598

    Re: Problems with CreatePen (WinGDI.h)

    Usually, old objects are saved when they are replaced with new objects. Later, at the end, new objects are replaced back by the previous objects, and the unselected new objects are then deleted. For exemple:
    Code:
    int paint_line(HWND hwnd, HDC hdc, int x0, int y0, int x9, int y9, int thickness, COLORREF rgb)
    {
       HBRUSH hbr; HPEN hpen, hpen_sav;
    
       hpen = CreatePen(PS_SOLID, thickness, rgb);
       hpen_sav = (HPEN) SelectObject(hdc, hpen);
       hbr = (HBRUSH)SelectObject(hdc, GetStockObject(NULL_BRUSH));
    
       MoveToEx(hdc, x0, y0, NULL);
       LineTo(hdc, x9, y9);
    
       DeleteObject(SelectObject(hdc, hbr));
       SelectObject(hdc, hpen_sav);
       DeleteObject(hpen);
    
       return 0;
    }
    Try also to remove objects in the reverse order of their creation, especially the removal of the display context should be near the end:
    Code:
    void SimplePlot::Stop()
    {
    DeleteObject(hFont);	
    DeleteObject(hBrush);	
    DeleteObject(hPen);	
    DeleteDC(hDC);
    EndPaint(hArea, &Ps);
    }

  8. #8
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: Problems with CreatePen (WinGDI.h)

    Each time you use the CreatePen (Brush, etc), you're creating a GDI object. Since you have your hPen, etc as a member variable, your CreatePen is overwriting the same hPen from your previous CreatePen, causing a GDI object leak.

    One suggestion is to create all the objects you need in one function (let's say OnInitialUpdate) and store the handle in your member variable.

    Next, in your drawing code, don't create more objects, simply select the ones you have into the DC, use them, and then unselect them (as olivthill2 said). This way, you're not re-creating objects each time your draw code is called.

    When you're all done drawing (perhaps in the destructor) you can now delete the objects.

    This should eliminate your GDI object leaks.

    Hope that helps.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured