Print Page | Close Window

GDI Resource leaks - CXTPDialogBaseImpl

Printed From: Codejock Forums
Category: Codejock Products
Forum Name: Command Bars
Forum Description: Topics Related to Codejock Command Bars
URL: http://forum.codejock.com/forum_posts.asp?TID=24536
Printed Date: 01 April 2025 at 9:22pm
Software Version: Web Wiz Forums 12.04 - http://www.webwizforums.com


Topic: GDI Resource leaks - CXTPDialogBaseImpl
Posted By: rdhd
Subject: GDI Resource leaks - CXTPDialogBaseImpl
Date Posted: 16 December 2024 at 8:47am
When constructing a dialog derived from CXTPDialogBaseImpl the base class creates a CXTPCommandBars object. In its constructor a CXTPCustomizeDropSource object is created and in its constructor 3 icons are loaded. Our GDI resource leak tool is identifying these as resource leaks. I ran the tool because I noticed the GDI count going up in our process whenever we launch a dialog that uses this base class. Where is the DestroyIcon code supposed to go. Looks like it should be in destructor which is devoid of code.

CXTPCustomizeDropSource::CXTPCustomizeDropSource(CXTPCommandBars* pCommandBars)
{
    m_hcurDelete   = XTPResourceManager()->LoadCursor(XTP_IDC_COMMANDBARS_DRAGDELETE);
    m_hcurMove     = XTPResourceManager()->LoadCursor(XTP_IDC_COMMANDBARS_DRAGMOVE);
    m_hcurCopy     = XTPResourceManager()->LoadCursor(XTP_IDC_COMMANDBARS_DRAGCOPY);
    m_pSheet       = NULL;
    m_pCommandBars = pCommandBars;

    m_hwndCapture = 0;
    m_pControl    = 0;
    m_bMove          = FALSE;
    m_bCopyOnly   = FALSE;
    m_pTarget     = NULL;
}

CXTPCustomizeDropSource::~CXTPCustomizeDropSource()
{
}

This is in the current release.



Replies:
Posted By: agontarenko
Date Posted: 17 December 2024 at 4:20am
Hello,

What GDI resource leak tool you used for identifying resource leaks?

Regards,
Artem Gontarenko


Posted By: agontarenko
Date Posted: 17 December 2024 at 4:22am
How I can to reproduce this?


Posted By: rdhd
Date Posted: 17 December 2024 at 9:33am
We have a tool we wrote that hooks various APIs and tracks resource allocations. It uses debugging APIs to capture call stacks on allocation. When it lists any outstanding allocations we can double click an entry it shows on a call stack and it opens the source file in Visual Studio. It lets us start tracking and stop tracking on demand. So, I started tracking, brought up a dialog derived from the CJ class, closed the dialog and stopped tracking. Then I listed all outstanding resources and saw the "icon" resources and followed the stack and code.

I fixed this in the destructor by checking for non-null members and calling DestroyIcon. Just set a breakpoint in the constructor and destructor and you can see the allocation and see in the destructor the members are non null.


Posted By: rdhd
Date Posted: 17 December 2024 at 10:43am
uploads/3213/CJLeak.zip" rel="nofollow - uploads/3213/CJLeak.zip

Video of ISpy detecting GdiCursor leak in toolkit pro.


Posted By: agontarenko
Date Posted: 18 December 2024 at 4:13am
Hello,

I've changed code by calling GetGuiResources before and after LoadIcon/DestroyCursor/DestroyIcon and GetGuiResources returns same value before and after DestroyCursor/DestroyIcon functions. So this functions not clean GDI resources after LoadIcon call.



Next I've more detail checked MSDN documentation and found one thing:

https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-destroycursor
Remarks :
The DestroyCursor function destroys a nonshared cursor. Do not use this function to destroy a shared cursor. A shared cursor is valid as long as the module from which it was loaded remains in memory. The following functions obtain a shared cursor: LoadCursor...

https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-destroyicon
In remarks of DestroyIcon function wrore same.
Remarks:
It is only necessary to call DestroyIcon for icons and cursors created with the following functions: CreateIconFromResourceEx (if called without the LR_SHARED flag), CreateIconIndirect, and CopyIcon. Do not use this function to destroy a shared icon. A shared icon is valid as long as the module from which it was loaded remains in memory. The following functions obtain a shared icon. LoadIcon...

So I thik this is not a bug.

Regards,
Artem Gontarenko


Posted By: rdhd
Date Posted: 18 December 2024 at 2:16pm
Artem,

You are correct. I failed to run our tool again while invoking our dialog multiple times after first seeing the GdiCursor entries. We do that second test to see if multiple entries build up. I just did so running three times and only one cursor entry showed up for each line instead of three. The handles that show up are constants so the system is not loading another cursor unless I destroy the previously loaded cursor.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.04 - http://www.webwizforums.com
Copyright ©2001-2021 Web Wiz Ltd. - https://www.webwiz.net