Solved

Small Memory Leak in Toolbar creation

Posted on 2004-04-30
6
456 Views
Last Modified: 2013-12-03
I use this code to create a toolbar & rebar for a dialogbox.

HIMAGELIST hL ; // handle to an imagelist that contains the bitmaps
void CreateToolbarForDialog(HWND hh,int numb,int Buttons[],int ids[],char* Texts[])
      {
   HWND MainRebar = 0;
   HWND MainToolbar = 0;
   MainRebar = CreateWindowEx(0,REBARCLASSNAME,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,hAppInstance,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_NODIVIDER | CCS_NORESIZE | TBSTYLE_TRANSPARENT | WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT | WS_VISIBLE,9902,0,0,0,0,0,20,20,20,20,sizeof(TBBUTTON));

   SendMessage(MainToolbar,TB_SETIMAGELIST,0,(LPARAM)hL);

   for(int i = 0 ; i < numb ; i++)
      tbButtons[i].iString = SendMessage(MainToolbar,TB_ADDSTRING,0,(LPARAM)Strings[i]);

   SendMessage(MainToolbar,TB_ADDBUTTONS,numb,(LPARAM)&tbButtons[0]);

   REBARBANDINFO rbBand;
   memset(&rbBand,0,sizeof(rbBand));
   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_SETIMAGELIST,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
0
Comment
Question by:WxW
6 Comments
 
LVL 16

Expert Comment

by:nonubik
ID: 10958827
For instance, try to SendMessage with TB_DELETEUTTON for each button you add.
0
 
LVL 6

Author Comment

by:WxW
ID: 10961742
It doesn't work. Suspect something goes wrong with the image lis\t, but what ?
0
 
LVL 6

Expert Comment

by:joghurt
ID: 10980611
Do you call ImageList_Destroy when you destroy the dialog?
0
 
LVL 6

Author Comment

by:WxW
ID: 10980992
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.

0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 11248555
PAQed, with points refunded (250)

modulo
Community Support Moderator
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question