tray menu leaking handles
hi guys,
my application has a tray icon with a tray menu.
problem is that in the process of showing the tray
menu and closing it, i loose about 3-6 GDI handles, consistently.
there is nothing in the code that points to this handle lose.
i might be wrong and really need some help here so i'm attaching the code fragment:
Code:
case WM_RBUTTONUP:
{
BOOL busyStatus = (BOOL)AfxGetMainWnd()->SendMessage( WM_IS_BUSY );
if( busyStatus ) {
return 0;
}
CMenu menu;
if (!menu.LoadMenu(m_niData.uID)) {
return 0;
}
CMenu* pSubMenu = menu.GetSubMenu(0);
if (pSubMenu == NULL) {
return 0;
}
const int n = theApp.GetAccountsCount();
const DWORD flags = ( n > 0 ? MF_ENABLED : MF_DISABLED | MF_GRAYED );
pSubMenu->SetDefaultItem( ID_RESTORE );
pSubMenu->EnableMenuItem( ID_CHECKNOW, MF_BYCOMMAND | flags );
pSubMenu->EnableMenuItem( ID_REMOVE, MF_BYCOMMAND | flags );
pSubMenu->EnableMenuItem( ID_ADD, n == MAX_ACCOUNTS ? MF_DISABLED | MF_GRAYED : MF_ENABLED );
CPoint pos;
GetCursorPos(&pos);
::SetForegroundWindow(m_hWndNotify);
::TrackPopupMenu(pSubMenu->m_hMenu, 0, pos.x, pos.y, 0, m_hWndNotify, NULL);
::PostMessage(m_hWndNotify, WM_NULL, 0, 0);
menu.DestroyMenu();
your help is appreciated !
Re: tray menu leaking handles
Hi,
Just a guess, but maybe the removal of the menu item is causing a problem.
Do you still get the leak without the EnableMenuItems calls?
Alan
Re: tray menu leaking handles
Quote:
Originally Posted by
neo_the_1
... my application has a tray icon with a tray menu.
problem is that in the process of showing the tray
menu and closing it, i loose about 3-6 GDI handles, consistently.
I see nothing in the posted code snippet that might cause handle leaks. I use almost the same code without any problem.
Perhaps, something wrong happens somewhere outside this code, for instance, in this call:
Quote:
Code:
const int n = theApp.GetAccountsCount();
or somewhere else. :confused:
Re: tray menu leaking handles
Handle leaking is probably located in WM_PAINT, or OnPaint, OnDraw functions...can you paste that code too?
Re: tray menu leaking handles
well, i dont really handle wm_paint or so.
as for the call to theApp.GetAccountsCount(), it just returns a vector size.
it's really annoying .. every time i show the menu .. booom i loose minimum 3 gdi handles.
Re: tray menu leaking handles
found it ! (not yet fixed it)
Code:
bool CTrayIcon::SetIcon(LPCTSTR lpszIconName)
{
int cx = ::GetSystemMetrics(SM_CXSMICON);
int cy = ::GetSystemMetrics(SM_CYSMICON);
m_hIcon = ( HICON )::LoadImage( AfxGetResourceHandle( ), lpszIconName, IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR );
return SetIcon( m_hIcon );
}
bool CTrayIcon::SetIcon(UINT nIDResource)
{
return SetIcon( MAKEINTRESOURCE( nIDResource ) );
}
the second SetIcon is called each time i right click on the tray.
it appears that there is no release for the previously allocated icon handle.
Re: tray menu leaking handles
As I already pointed out:
Quote:
Originally Posted by
VictorN
Perhaps, something wrong happens somewhere outside this code, ... :confused:
So, I glad you found it! :)