473,383 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Memory Leak (40K per mouse over)

I'm currently writing a replacement shell and am suffering from quite dire memory leaks, when running, every time I roll my mouse over the taskbar icon a new window scrolls out revealing the window's caption and then on WM_MOUSELEAVE the window closes again. somehow this process also increases my program's private working set by 40K (using SysInternals' Process Explorer) Any help would be appreciated.

Extract from TaskbarItem.cpp:

Expand|Select|Wrap|Line Numbers
  1. LRESULT CALLBACK            TaskbarItem::LocalProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam){
  2.     switch(msg){
  3.         case WM_MOUSEMOVE:{
  4.             if(this->caption==(TaskExpander*)NULL)
  5.                 this->caption=new TaskExpander(&(*this));
  6.             else
  7.                 this->caption->Reopen();
  8.         }break;
  9.         case WM_MOVED:
  10.             if(caption!=(TaskExpander*)NULL){
  11.                 SendMessage(caption->GetDispHWnd(),WM_MOVED,0,0);
  12.             }
  13.             break;
  14.         default:return DefWindowProc(hWnd,msg,wParam,lParam);
  15.     }
  16.     return 0L;
  17. }
Extract from TaskExpander.cpp:
Expand|Select|Wrap|Line Numbers
  1. void                                TaskExpander::Reopen        (){
  2.     if((state & _O)==0){
  3.         KillTimer(dWnd,IDT_TIMER_CLOSE);
  4.         KillTimer(dWnd,IDT_TIMER_TIMEOUT);
  5.         state=OPENING;
  6.         SetTimer(dWnd,IDT_TIMER_OPEN,ANI_TIME,0);
  7.         ShowWindow(dWnd,SW_SHOW);
  8.     }
  9. }
  10. LRESULT CALLBACK                    TaskExpander::LocalProc        (HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam){
  11.     LPPAINTSTRUCT ps=new PAINTSTRUCT;
  12.     HDC hdc;
  13.     HFONT hFont;
  14.     hFont=global::font::basic;
  15.     switch(msg){        
  16.         case WM_MOVED:{
  17.             LPRECT r=new RECT;
  18.             LPRECT s=new RECT;
  19.             GetWindowRect(pTbi->GetDispHWnd(),r);
  20.             GetWindowRect(hWnd,s);
  21.             position.x=r->right;
  22.             position.y=r->top-1;
  23.             MoveWindow(hWnd,position.x-(s->right-s->left),position.y,(r->right-r->left),r->bottom-r->top,true);
  24.             InvalidateRgn(hWnd,NULL,TRUE);
  25.         }break;
  26.         case WM_PAINT:{
  27.             hdc=BeginPaint(hWnd,ps);
  28.             SetBkColor(hdc,0x00000000);
  29.             SelectObject(hdc,hFont);
  30.             SetTextColor(hdc,0x00FFFFFF);
  31.             size_t psz;
  32.             LPRECT r=new RECT;
  33.             GetWindowRect(hWnd,r);
  34.             HICON icon=pTbi->GetIcon();
  35.             StringCchLength(pTbi->GetTitle(),MAX_LOADSTRING,&psz);
  36.             TextOut(hdc,2*ICON_GAP+ICON_SIZE,ICON_GAP/2,pTbi->GetTitle(),psz);
  37.             DrawIconEx(hdc,ICON_GAP,0,icon,ICON_SIZE,ICON_SIZE,0,NULL,DI_NORMAL);
  38.             EndPaint(hWnd,ps);
  39.             delete r;
  40.             }break;
  41.         case WM_TIMER:
  42.             switch(wParam){
  43.                 case IDT_TIMER_OPEN:{
  44.                     state=OPENING;
  45.                     hdc=GetDC(hWnd);
  46.                     LPRECT r=new RECT;
  47.                     size_t psz;
  48.                     SIZE size;
  49.                     SelectObject(hdc,hFont);
  50.                     StringCchLength(pTbi->GetTitle(),MAX_LOADSTRING,&psz);
  51.                     GetTextExtentPoint32(hdc,pTbi->GetTitle(),*&psz,&size);
  52.                     GetWindowRect(hWnd,r);
  53.                     if((size.cx+(2*ICON_GAP)+ICON_SIZE)-(r->right-r->left)>ANI_STEP)
  54.                         MoveWindow(hWnd,position.x-(r->right-r->left)-ANI_STEP,position.y,(r->right-r->left)+ANI_STEP,r->bottom-r->top,true);
  55.                     else{
  56.                         MoveWindow(hWnd,position.x-(size.cx+(2*ICON_GAP)+ICON_SIZE),position.y,(size.cx+(2*ICON_GAP)+ICON_SIZE),r->bottom-r->top,true);
  57.                         KillTimer(hWnd,IDT_TIMER_OPEN);
  58.                         SetTimer(hWnd,IDT_TIMER_TIMEOUT,10000,(TIMERPROC)NULL);
  59.                         state=OPEN;
  60.                     }
  61.                     delete r;
  62.                 }break;
  63.                 case IDT_TIMER_CLOSE:{
  64.                     state=CLOSING;
  65.                     KillTimer(hWnd,IDT_TIMER_TIMEOUT);
  66.                     LPRECT r=new RECT;
  67.                     GetWindowRect(hWnd,r);
  68.                     MoveWindow(hWnd,position.x-(r->right-r->left)+ANI_STEP,position.y,(r->right-r->left)-ANI_STEP,r->bottom-r->top,true);
  69.                     if(r->right-r->left <= ICON_SIZE+ANI_STEP){
  70.                         KillTimer(hWnd,IDT_TIMER_CLOSE);
  71.                         state=READY;
  72.                         ShowWindow(hWnd,SW_HIDE);
  73.                     }
  74.                     delete r;
  75.                 }break;
  76.                 case IDT_TIMER_TIMEOUT:{
  77.                     KillTimer(hWnd,IDT_TIMER_OPEN);
  78.                     this->state=CLOSING;
  79.                     SetTimer(hWnd,IDT_TIMER_CLOSE,ANI_TIME,(TIMERPROC)NULL);
  80.                 }break;
  81.             }break;
  82.         case WM_INIT:
  83.             ShowWindow(dWnd,SW_SHOW);
  84.             SetTimer(hWnd,IDT_TIMER_OPEN,ANI_TIME,(TIMERPROC)NULL);break;
  85.             state=OPENING;
  86.             break;
  87.         case WM_MOUSEMOVE:
  88.             if(state==CLOSING){
  89.                 KillTimer(hWnd,IDT_TIMER_CLOSE);
  90.                 SetTimer(hWnd,IDT_TIMER_OPEN,ANI_TIME,(TIMERPROC)NULL);
  91.             }
  92.             if(!tracking){
  93.                 TRACKMOUSEEVENT tme;
  94.                 tme.cbSize=sizeof(tme);
  95.                 tme.dwFlags=TME_LEAVE;
  96.                 tme.hwndTrack=hWnd;
  97.                 TrackMouseEvent(&tme);
  98.                 tracking=true;
  99.             }
  100.             break;
  101.         case WM_MOUSELEAVE:
  102.             KillTimer(hWnd,IDT_TIMER_OPEN);
  103.             KillTimer(hWnd,IDT_TIMER_TIMEOUT);
  104.             SetTimer(hWnd,IDT_TIMER_CLOSE,ANI_TIME,0);
  105.             state=CLOSING;
  106.             tracking=false;
  107.             break;
  108.         case WM_LBUTTONDOWN:
  109.             SetForegroundWindow(pTbi->GetHWnd());
  110.             SetActiveWindow(pTbi->GetHWnd());
  111.             break;
  112.         default:delete ps,hdc,hFont;return DefWindowProc(hWnd,msg,wParam,lParam);
  113.     }
  114.     return 0L;
  115. }
Dec 1 '08 #1
5 2784
Banfa
9,065 Expert Mod 8TB
I see the code allocating TaskExpanders

this->caption=new TaskExpander(&(*this));

but never deleting them and I see the code deleting pointers

default:delete ps,hdc,hFont;return DefWindowProc(hWnd,msg,wParam,lParam);

that have not had memory allocated to them while most of the time it doesn't delete

LPPAINTSTRUCT ps=new PAINTSTRUCT;

allocated PAINTSTRUCTs
Dec 1 '08 #2
Thank you for your response.

@Banfa
They would be deleted in the destructor for the TaakbarItem, however this should only cause the memory to go up on the first call surely? memory goes up every time the expander reopens.

adding the "delete ps;" in the relevant places has roughly halved the memory leaking. I managed to find the main cause to be the GetDC() on line 45, adding a corresponding releaseDC has solved the problem.
Thank you for your help.
Dec 1 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
You are creating new RECT variables in several places. How are those cleaned up?
Dec 1 '08 #4
@weaknessforcats
Those are now all being deleted after use.

Thank you for your help.
This aspect of the program no longer leaks, the rest of the program has the memory management of a sieve but i should be able to sort that out. Thank you.
Dec 1 '08 #5
Banfa
9,065 Expert Mod 8TB
I don't know if this is code you inherited or wrote but personally whenever I allocate memory from the heap using new I always put in the corresponding delete immediately before filling in any other code.

You may also want to consider using the handle template
Dec 1 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: cyberstrike | last post by:
okay, I have a wierd problem. first of all, when my app loads, it uses a lot of memory. Most of which goes away if I minimize and reopen the application. Second problem is that I have a datagrid on...
8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
3
by: Chris Oates | last post by:
I'm using interop to bring in user32.dll and gdi32.dll, and I'm finding that I appear to have some kind of memory leak in the CLR. use of GC.GetTotalMemory() shows a lot of motion, but it does not...
1
by: Perry | last post by:
Hi, I have a problem with the memory consumption of the AppDomain.Load and unload. For example, when i run this code, the memory usage is increasing. MessageBox.Show(string.Format("Before:...
9
by: Anton | last post by:
{Willy Skjveland} Hi, how can I trace a Memory leak in aspnet_wp.exe? {Rheena} One moment please while I search it for you. It may take me a few moments {Willy Skjveland} I need to find out...
8
by: Adrian | last post by:
Hi I have a JS program that runs localy (under IE6 only) on a PC but it has a memory leak (probably the known MS one!) What applications are there that I could use to look at the memory usage of...
6
by: eduard.antonyan | last post by:
All I'm doing is going through the webpage and analyzing the data, here's the outline of the code: in OnDocumentComplete(LPCTSTR lpszURL) { IDispatch *pDispatch = GetHtmlDocument();...
22
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a...
18
by: Daniel Orner | last post by:
Hi all, I've been trying to pin down a memory leak in IE6 for several WEEKS now. I've done my share of googling etc., and know all about common leaks like circular references and closures, but...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.