Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > Visual C++
Visual C++ Questions specific to Microsoft's Visual C++. For questions not specific to this Microsoft version, use the C++ Programming forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual C++ section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old August 22nd, 2011, 05:56 AM
Authorized User
Join Date: Aug 2010
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
Default How to deal with memory leak in MFC?

Hello.I define a class based on CView in my MFC application.Let's call the class MyClass.In MyClass.h file I define a pointer with this code "CDC* m_pDC;"And in the OnCreate member function in MyClass.cpp,I type "m_pDC = new CClientDC(this);"The code can be built successfuly.But it can not run.The VS2008 IDE just exit with some tip in output window that there are some memory leaks.And the first memory leak is dericted to the former code "m_pDC = new CClientDC(this);".How can I fix them up?
Old December 7th, 2011, 01:23 AM
Registered User
Join Date: Oct 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts

you can steer clear of memory drip in cases where modeless discussion allocated around the heap by utilizing overriding PostNcDestroy.

Dialogs are not designed for auto-cleanup ( while Main frame windows, View home windows are). In case you want to provide the actual auto-cleanup with regard to dialogs then you definitely must override the PostNcDestroy fellow member function in your derived course. To include auto-cleanup to your class, call your own foundation class after which perform a remove this. To get rid of auto-cleanup out of your class, phone CWnd::PostNcDestroy directly rather than the PostNcDestroy fellow member inside your direct foundation course.

void MyDialog::PostNcDestroy()

delete this;

The salt water evaporates (through MSDN):

When wrecking a Home windows windowpane, the final Windows message sent to the window is actually WM_NCDESTROY. The default CWnd handler for your message (CWnd::OnNcDestroy) will detach the actual HWND in the C++ item and call the digital perform PostNcDestroy. Some classes override this particular perform to delete the C++ object.

"delete this" will free any kind of C++ memory linked to the C++ item. Although the fall behind CWnd destructor phone calls DestroyWindow if m_hWnd is actually non-NULL, this doesn't result in infinite recursion since the handle will be detached and NULL during the cleanup phase.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory leak in a loop neilsands Java Basics 4 March 17th, 2010 05:59 AM
Looking for Memory Leak Advice Bill_Thompson Visual C++ 0 October 16th, 2007 05:42 PM
IE Memory Leak Jman82 BOOK: Professional Ajax ISBN: 978-0-471-77778-6 0 July 6th, 2006 05:22 PM
Memory Leak in ADO Seeja Pro VB Databases 1 December 27th, 2004 07:28 PM
Think I have a memory leak, need advice grantmeans Classic ASP Databases 1 March 12th, 2004 03:45 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.