WxW
asked on
Small Memory Leak in Toolbar creation
I use this code to create a toolbar & rebar for a dialogbox.
HIMAGELIST hL ; // handle to an imagelist that contains the bitmaps
void CreateToolbarForDialog(HWN D hh,int numb,int Buttons[],int ids[],char* Texts[])
{
HWND MainRebar = 0;
HWND MainToolbar = 0;
MainRebar = CreateWindowEx(0,REBARCLAS SNAME,0,WS _BORDER | RBS_VARHEIGHT | RBS_AUTOSIZE | RBS_FIXEDORDER | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | CCS_NODIVIDER | CCS_NOPARENTALIGN | WS_VISIBLE | WS_BORDER | WS_CHILD,0, 0, 2000, 60,hh,(HMENU)9901,hAppInst ance,0);
TBBUTTON tbButtons[32];
for(int i = 0 ; i < 32 ; i++)
{
tbButtons[i].iBitmap = Buttons[i];
tbButtons[i].idCommand = ids[i];
tbButtons[i].fsState = TBSTATE_ENABLED;
tbButtons[i].fsStyle = TBSTYLE_BUTTON;
tbButtons[i].dwData = 0;
if (ids[i] == -1)
tbButtons[i].fsStyle |= TBSTYLE_SEP;
}
MainToolbar = CreateToolbarEx(hh,CCS_NOD IVIDER | CCS_NORESIZE | TBSTYLE_TRANSPARENT | WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT | WS_VISIBLE,9902,0,0,0,0,0, 20,20,20,2 0,sizeof(T BBUTTON));
SendMessage(MainToolbar,TB _SETIMAGEL IST,0,(LPA RAM)hL);
for(int i = 0 ; i < numb ; i++)
tbButtons[i].iString = SendMessage(MainToolbar,TB _ADDSTRING ,0,(LPARAM )Strings[i ]);
SendMessage(MainToolbar,TB _ADDBUTTON S,numb,(LP ARAM)&tbBu ttons[0]);
REBARBANDINFO rbBand;
memset(&rbBand,0,sizeof(rb Band));
rbBand.cbSize = sizeof(REBARBANDINFO);
rbBand.fMask = RBBIM_COLORS | // clrFore and clrBack are valid
RBBIM_CHILD | // hwndChild is valid
RBBIM_CHILDSIZE | // cxMinChild and cyMinChild are valid
RBBIM_STYLE | // fStyle is valid
RBBIM_ID | // wID is valid
RBBIM_BACKGROUND; // hbmBack is valid
rbBand.hbmBack = 0;
rbBand.clrFore = GetSysColor(COLOR_BTNTEXT) ;
rbBand.clrBack = GetSysColor(COLOR_BTNFACE) ;
rbBand.fStyle = RBBS_NOVERT | // do not display in vertical orientation
RBBS_FIXEDSIZE |
RBBS_FIXEDBMP;
rbBand.hwndChild = MainToolbar;
rbBand.wID = 1001;
rbBand.cxMinChild = 0;
rbBand.cyMinChild = 40;
rbBand.cyMaxChild = 450;
// Insert band into rebar
::SendMessage(MainRebar, RB_INSERTBAND, (UINT) -1, (LPARAM) (LPREBARBANDINFO) &rbBand);
}
When I finish with the dialog, I use SendMessage(hT,TB_SETIMAGE LIST,0,0) to clear the imagelist from the toolbar hT before destroying the dialog. Each time the dialog is created, I see in the Process Manager more and more memory to be reserved (16K each time).
What am I doing wrong ?
Thanks
HIMAGELIST hL ; // handle to an imagelist that contains the bitmaps
void CreateToolbarForDialog(HWN
{
HWND MainRebar = 0;
HWND MainToolbar = 0;
MainRebar = CreateWindowEx(0,REBARCLAS
TBBUTTON tbButtons[32];
for(int i = 0 ; i < 32 ; i++)
{
tbButtons[i].iBitmap = Buttons[i];
tbButtons[i].idCommand = ids[i];
tbButtons[i].fsState = TBSTATE_ENABLED;
tbButtons[i].fsStyle = TBSTYLE_BUTTON;
tbButtons[i].dwData = 0;
if (ids[i] == -1)
tbButtons[i].fsStyle |= TBSTYLE_SEP;
}
MainToolbar = CreateToolbarEx(hh,CCS_NOD
SendMessage(MainToolbar,TB
for(int i = 0 ; i < numb ; i++)
tbButtons[i].iString = SendMessage(MainToolbar,TB
SendMessage(MainToolbar,TB
REBARBANDINFO rbBand;
memset(&rbBand,0,sizeof(rb
rbBand.cbSize = sizeof(REBARBANDINFO);
rbBand.fMask = RBBIM_COLORS | // clrFore and clrBack are valid
RBBIM_CHILD | // hwndChild is valid
RBBIM_CHILDSIZE | // cxMinChild and cyMinChild are valid
RBBIM_STYLE | // fStyle is valid
RBBIM_ID | // wID is valid
RBBIM_BACKGROUND; // hbmBack is valid
rbBand.hbmBack = 0;
rbBand.clrFore = GetSysColor(COLOR_BTNTEXT)
rbBand.clrBack = GetSysColor(COLOR_BTNFACE)
rbBand.fStyle = RBBS_NOVERT | // do not display in vertical orientation
RBBS_FIXEDSIZE |
RBBS_FIXEDBMP;
rbBand.hwndChild = MainToolbar;
rbBand.wID = 1001;
rbBand.cxMinChild = 0;
rbBand.cyMinChild = 40;
rbBand.cyMaxChild = 450;
// Insert band into rebar
::SendMessage(MainRebar, RB_INSERTBAND, (UINT) -1, (LPARAM) (LPREBARBANDINFO) &rbBand);
}
When I finish with the dialog, I use SendMessage(hT,TB_SETIMAGE
What am I doing wrong ?
Thanks
For instance, try to SendMessage with TB_DELETEUTTON for each button you add.
ASKER
It doesn't work. Suspect something goes wrong with the image lis\t, but what ?
Do you call ImageList_Destroy when you destroy the dialog?
ASKER
Yes.
I found the problem.
I should use CreateWindowEx(). CreateToolbarEx() does something that allocates memory (internal leak?) and does not free it after I destroy the dialog.
I found the problem.
I should use CreateWindowEx(). CreateToolbarEx() does something that allocates memory (internal leak?) and does not free it after I destroy the dialog.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.