CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2002
    Posts
    372

    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 !
    Last edited by neo_the_1; May 26th, 2009 at 02:17 AM.

  2. #2
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    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

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: tray menu leaking handles

    Quote Originally Posted by neo_the_1 View Post
    ... 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:
    Code:
    		const int n = theApp.GetAccountsCount();
    or somewhere else.
    Last edited by VictorN; May 26th, 2009 at 03:30 AM.
    Victor Nijegorodov

  4. #4
    Join Date
    Feb 2009
    Posts
    42

    Re: tray menu leaking handles

    Handle leaking is probably located in WM_PAINT, or OnPaint, OnDraw functions...can you paste that code too?

  5. #5
    Join Date
    Jul 2002
    Posts
    372

    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.

  6. #6
    Join Date
    Jul 2002
    Posts
    372

    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.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: tray menu leaking handles

    As I already pointed out:
    Quote Originally Posted by VictorN View Post
    Perhaps, something wrong happens somewhere outside this code, ...
    So, I glad you found it!
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured