Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / May 2005

Tip: Looking for answers? Try searching our database.

new and delete problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Berrywong - 01 May 2005 07:10 GMT
I creat a view in MyDlg using
m_pview=new MyView();

I am not sure where I should delete m_pview.

I called destroywindow in MyView and MyDlg,  I have tried to delete m_pview
in destructor of MyView or MyDlg or PostNcDestroy() function , but all failed
because "Access violation" at line "delete m_pview".
If I don't delete m_pview, there is "memory leak" at line "m_pview=new
MyView()".

I have no idea about that.  Any help or suggestion is appreciated!
Tamas Demjen - 02 May 2005 21:32 GMT
> I creat a view in MyDlg using
> m_pview=new MyView();
[quoted text clipped - 6 lines]
> If I don't delete m_pview, there is "memory leak" at line "m_pview=new
> MyView()".

Probably you deleted it twice. It should only be deleted once, and from
the same class where it is created. If you create it from MyDlg, delete
it from MyDlg. You should try to set it to 0 after deleting it, to
prevent accidental double deletions:

delete m_pview; m_pview = 0;

Even better, use std::auto_ptr or boost::scoped_ptr instead of native
pointers. They're way much safer:

#include "MyView.h"
class MyDlg
{
private:
   std::auto_ptr<MyView> view;
};

then instead of
m_pview = new MyView;

do

view.reset(new MyView);

and never worry about deletion.

Tom

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.