Codejock Forums Homepage
Forum Home Forum Home > Codejock Products > Visual C++ MFC > Command Bars
  New Posts New Posts RSS Feed - CXTPImageManager Memory Leak?
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

CXTPImageManager Memory Leak?

 Post Reply Post Reply
Author
Message
Danlar View Drop Down
Groupie
Groupie
Avatar

Joined: 27 February 2008
Location: United States
Status: Offline
Points: 12
Post Options Post Options   Thanks (0) Thanks(0)   Quote Danlar Quote  Post ReplyReply Direct Link To This Post Topic: CXTPImageManager Memory Leak?
    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
Back to Top
Danlar View Drop Down
Groupie
Groupie
Avatar

Joined: 27 February 2008
Location: United States
Status: Offline
Points: 12
Post Options Post Options   Thanks (0) Thanks(0)   Quote Danlar Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
Oleg View Drop Down
Admin Group
Admin Group


Joined: 21 May 2003
Location: United States
Status: Offline
Points: 11234
Post Options Post Options   Thanks (0) Thanks(0)   Quote Oleg Quote  Post ReplyReply Direct Link To This Post Posted: 27 February 2008 at 1:08pm
Hi,
LoadBitmapFromResource created BITMAP that you had to destroy.
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS
Back to Top
Danlar View Drop Down
Groupie
Groupie
Avatar

Joined: 27 February 2008
Location: United States
Status: Offline
Points: 12
Post Options Post Options   Thanks (0) Thanks(0)   Quote Danlar Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.04
Copyright ©2001-2021 Web Wiz Ltd.

This page was generated in 0.141 seconds.