Print Page | Close Window

CXTPImageManager Memory Leak?

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=9723
Printed Date: 28 March 2024 at 12:57pm
Software Version: Web Wiz Forums 12.04 - http://www.webwizforums.com


Topic: CXTPImageManager Memory Leak?
Posted By: Danlar
Subject: CXTPImageManager Memory Leak?
Date Posted: 27 February 2008 at 12:26pm
I'm executing code which removes icons from the image manager and adds new ones in their place every time a ribbon control gets updated. The memory used by the application climbs very quickly while this happens. Visual Studio doesn't report any memory leaks when the program exits, though, so whatever is allocating all of them is cleaning them up during the exit.

Consider this code which sometimes will execute very very frequently:

void CMyXTPFrameWnd::ChangeControlIcon(CXTPControl* control, int icon16, int icon32)

{
    ASSERT(control != NULL);
    BOOL alpha;
    int controlID = control->GetID();

    this->GetCommandBars()->GetImageManager()->RemoveIcon(controlID);

    if (icon16 != 0) {
        this->GetCommandBars()->GetImageManager()->SetIcon(CXTPImageManagerIcon::LoadBitmapFromResource(MAKEINTRESOURCE(icon16), &alpha), controlID, CSize(16, 16), xtpImageNormal);
    }
    if (icon32 != 0 ) {
        this->GetCommandBars()->GetImageManager()->SetIcon(CXTPImageManagerIcon::LoadBitmapFromResource(MAKEINTRESOURCE(icon32), &alpha), controlID, CSize(32, 32), xtpImageNormal);
    }
    control->RedrawParent(TRUE);
}


When this code is executing, the memory usage by my application climbs very quickly. If I remove the calls to RemoveIcon and SetIcon, the memory stops climbing.

Any idea what's going on?

Before anyone asks, I am certain that control->getID() is returning the same value every time this function is getting executed. This rules out the option of constantly removing zero icons and adding two more.

Also I've tried throwing this->GetCommandBars()->GetImageManager()->GetImages()->GetCount() into the function in various places, and it does decrease in value and re-increase exactly as I'd expect.

-Dan



Replies:
Posted By: Danlar
Date Posted: 27 February 2008 at 12:47pm
Well, I fixed it. But, I'm not sure why or how. It's probably something very simple I've overlooked.

If I replace my SetIcon calls with SetIcons instead, everything gets better.

I changed...
SetIcon(CXTPImageManagerIcon::LoadBitmapFromResource(MAKEINTRESOURCE(icon16), &alpha, controlID, CSize(16, 16), xtpImageNormal)



into...
SetIcons(icon16, &controlID, 1, CSize(16, 16), xtpImageNormal)

and the memory usage has stopped climbing.

I've either missed a fundamental feature of MAKEINTRESOURCE or LoadBitmapFromResource, or I really don't understand the difference between SetIcon and SetIcons.

Thanks in advance for any insight.

-Dan


Posted By: Oleg
Date Posted: 27 February 2008 at 1:08pm
Hi,
LoadBitmapFromResource created BITMAP that you had to destroy.


-------------
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS


Posted By: Danlar
Date Posted: 27 February 2008 at 1:36pm
So I added code to my function above to delete the HBITMAP for each of the icons before removing them...

BOOL success = ::DeleteObject(this->GetCommandBars()->GetImageManager()->GetIconSet(controlID)->GetIcon(16, FALSE)->GetIcon().GetBitmap());

success = ::DeleteObject(this->GetCommandBars()->GetImageManager()->GetIconSet(controlID)->GetIcon(32, FALSE)->GetIcon().GetBitmap());
this->GetCommandBars()->GetImageManager()->RemoveIcon(controlID);


DeleteObject returns TRUE each time, indicating it did successfully delete the HBITMAP.

This doesn't fix the constantly growing amount of memory though.



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