Home Dashboard Directory Help
Search

Memory Leaks in MFC Class by Saurabh Jog


Status: 

Closed
 as Fixed Help for as Fixed


4
0
Sign in
to vote
Type: Bug
ID: 613377
Opened: 10/13/2010 5:44:41 PM
Access Restriction: Public
0
Workaround(s)
view
4
User(s) can reproduce this bug

Description

When you add a CMFCButton to any dialog, the CToolTipCtrl pointer is initialized through the tooltipmanager class.
This CToolTipCtrl has a CMapStringToPtr member (m_mapString).
This map is not getting destroyed as the CToolTipCtrl is never DELETED.
This cause the application to have memory leaks.

This is really messy, coz I have a big application, and the aggregate memory leaks in my application is appalling.

Please let me know how to get around this problem.
Details
Sign in to post a comment.
Posted by WAMFAL on 2/28/2013 at 9:11 AM
See below link:

http://connect.microsoft.com/VisualStudio/feedback/details/577870/cmfcbutton-causes-memory-leak
Posted by David Carr on 10/29/2012 at 2:26 PM
This problem persists in VS2010 V10.0.40219.1 SP1Rel. From Saurabh Jog's post on 10/14/2010 at 5:10 PM, it is not necessary to attach a variable to the given CMFCButton - simply having this control on a dialog in the Resource View is sufficient to cause the crash in _CrtIsValidHeapPointer() (dbgheap.c) as it attempts to free the memory of the tooltip (even if the tooltip is empty!).

Presumably MS is the one to have marked this 'Closed as Fixed'. Can they be decent to please point to:
a) the link describing what the fix is
b) the version it is fixed in

Certainly this kind of basic information is required with any professional software development I do - I would expect this as a minimum standard from MS (even if 20 years of historical record proves otherwise).
Posted by Bookshpan on 9/13/2012 at 8:14 AM
This is easily reporducable.

My workaround is to remove the CMFCButton member variable together with the corresponding DDX_Control(...), and every time I need to access the button I use:

CMFCButton *pBtn = (CMFCButton *)GetDlgItem(IDC_MY_CMFCButton);
if (pBtn)
.........
Posted by NativeManMFC on 1/31/2012 at 5:51 AM
I wish you guys would fix this. When I run in debug and I exit I want to see if I have introduced any memory leaks. When your leaks are listed, I have to make sure they are in fact your leaks and not mine. It would be so much easier if I only had to look to see if I caused any leaks.
Posted by Vardenis on 12/8/2011 at 4:11 AM
vs2010 SP1 - not fixed
Posted by Andrew Bates 85 on 9/14/2011 at 2:35 AM
Well, I guess so too ...
i found a hack fix for this for anyone interested since it took me 2 weeks to solve this issue. note this is not by me i'm just relaying my information found here.

http://www.experts-exchange.com/Programming/Languages/CPP/Q_27191841.html

the information below is a copied answer and not my own words.

If you really want to get rid of these memory leaks for now here's a possible workaround (which is really a hack - generally I wouldn't suggest to do something like this):

Add these two declarations to your dialogs class (in my sample this is 'CTestDlgDlg'):

    void    WorkaroundMFCBug();
    bool    m_bWorkaroundDone;
Toggle HighlightingOpen in New WindowSelect All

Then add/change code to look somehow like this:

CTestDlgDlg::CTestDlgDlg(CWnd* pParent /*=NULL*/)
    : CDialogEx(CTestDlgDlg::IDD, pParent)
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    m_bWorkaroundDone = false;
}

// this class is only used to allow accessing the protected CMFCButton::m_pToolTip - don't use it for anything else
class CMFCButtonTooltipAccessHelper : public CMFCButton
{
public:
    using CMFCButton::m_pToolTip;
};

void CTestDlgDlg::WorkaroundMFCBug()
{
    if ( NULL == GetSafeHwnd() )
    {
        return;
    }

    if ( false != m_bWorkaroundDone )
    {
        return;
    }

    CWnd* pWndChild = GetWindow(GW_CHILD);
    while ( NULL != pWndChild )
    {
        CMFCButtonTooltipAccessHelper* pButton = static_cast< CMFCButtonTooltipAccessHelper* >( DYNAMIC_DOWNCAST( CMFCButton, pWndChild ) );

        if ( NULL != pButton && NULL != pButton->m_pToolTip )
        {
            CTooltipManager::DeleteToolTip( pButton->m_pToolTip );
            delete pButton->m_pToolTip;
            pButton->m_pToolTip = NULL;
        }

        pWndChild = pWndChild->GetNextWindow();
    }

    m_bWorkaroundDone = true;
}

void CTestDlgDlg::DoDataExchange(CDataExchange* pDX)
{
#pragma message ( "IMPORTANT: Check if this workaround is still needed with newer versions of MFC!" )
    WorkaroundMFCBug();

    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_MFCBUTTON1, m_btn1);
}
Toggle HighlightingOpen in New WindowSelect All

This workaround removes and deletes the tooltip internally created before the first 'DDX_Control' call (thus removing the memory leak).

ZOPPO

Posted by Andrew Bates 85 on 9/12/2011 at 8:11 AM
I Agree with Jason Sandys, i installed SP1 for VS2010 on Friday expecting this fix but still memory leaks. perhaps this is a hot fix?


f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\plex.cpp(29) : {10152} normal block at 0x0353CEB0, 164 bytes long.
Data: <             x> 00 00 00 00 00 00 00 00 C5 9D 1C 81 F8 B8 09 78
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\map_sp.cpp(83) : {10151} normal block at 0x0353D010, 68 bytes long.
Data: <     S         > 00 00 00 00 B4 CE 53 03 00 00 00 00 00 00 00 00
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\afxtooltipctrl.cpp(39) : {10150} client block at 0x0353D4D8, subtype c0, 252 bytes long.
a CMFCToolTipCtrl object at $0353D4D8, 252 bytes long
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\plex.cpp(29) : {10072} normal block at 0x0353D0C8, 164 bytes long.
Data: <             x> 00 00 00 00 00 00 00 00 C5 9D 1C 81 F8 B8 09 78
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\map_sp.cpp(83) : {10071} normal block at 0x0353C748, 68 bytes long.
Data: <     S         > 00 00 00 00 CC D0 53 03 00 00 00 00 00 00 00 00
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\afxtooltipctrl.cpp(39) : {10070} client block at 0x0353C898, subtype c0, 252 bytes long.
a CMFCToolTipCtrl object at $0353C898, 252 bytes long
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\plex.cpp(29) : {9990} normal block at 0x0353C1C0, 164 bytes long.
Data: <             x> 00 00 00 00 00 00 00 00 C5 9D 1C 81 F8 B8 09 78
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\map_sp.cpp(83) : {9989} normal block at 0x0353C458, 68 bytes long.
Data: <     S         > 00 00 00 00 C4 C1 53 03 00 00 00 00 00 00 00 00
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\afxtooltipctrl.cpp(39) : {9988} client block at 0x0353C320, subtype c0, 252 bytes long.
a CMFCToolTipCtrl object at $0353C320, 252 bytes long

Posted by Jason Sandys on 9/8/2011 at 8:18 PM
This is marked as Fixed and closed. Fixed where and when? I have VS2010 SP1 and am still experiencing the exact same symptoms using an MFCButton.
Posted by Microsoft on 10/15/2010 at 1:44 AM
Thanks for your feedback.
We are routing this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by Saurabh Jog on 10/14/2010 at 5:10 PM
Thanks for your response. I truly appreciate it.

The thing that I forgot to mention in my: STEPS TO REPRODUCE THE PROBLEM : is that you need to add a member variable for the MFCButton in your dialog class. You can use the standard Classwizard to add that.

Only when you create a member variable, you get the memory leaks.

I have attached a sample project in here. After you start the app, press ALT + F and B to start the ABOUT Dialog.

I have put a sample MFC Button in there. You don't need to do anything in the Dialog. Just Cancel the dialog once its displayed and then close the app.

In your OUTPUT window of VS IDE, you should see the memory leaks which I have mentioned in this Bug Report.

Thanks in advance...
Hope this helps...
Posted by Microsoft on 10/14/2010 at 12:10 AM
Thank you for reporting this issue. In order to research the issue reported, we must first reproduce in our labs. Unfortunately, we are unable to reproduce the issue with the steps you provided.

Could you please provide us with a sample project zip?

It would be greatly appreciated if you could provide us this information as quickly as possible.

Thank you,
Posted by Microsoft on 10/13/2010 at 7:57 PM
Thank you for your feedback, We are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.
File Name Submitted By Submitted On File Size  
MemLeakBugSample.zip 10/14/2010 346 KB