   HelP! (Access Violation ) delete pointer
  I have 2 classes , one is base class and the another one is derive class.class CCharacter{protected:CMakePolygon*p_tex;CLoadTextureClass*t_tex;intstatus;public:CCharacter(void);virtual ~CCharacter(void){CleanUp();}virtual void Init();virtual void Draw();virtual void Main();virtual void CleanUp(){delete p_tex; delete t_tex;}void SetStatus(int n){status = n;}};class CLoading: public CCharacter{private:intFPS;intround;public:CLoading(void);~CLoading(void){CleanUp();}void Init();void Main();void Animation();};the program works but when I close the program I got Access Violation: 0xC0000005: on CCharacter::CleanUp function What did I do wrong?   
  CleanUp is being called twice.... once in the inherited destructor and  in the base destructor... So the pointers are being deleted twice, hence the access violation... You can take it out of one of the destructor's or make the pointers null after you delete them... that way it won't matter if you call delete on them again...   ;Quote:Original post by PsycrosisCleanUp is being called twice.... once in the inherited destructor and  in the base destructor... So the pointers are being deleted twice, hence the access violation... You can take it out of one of the destructor's or make the pointers null after you delete them... that way it won't matter if you call delete on them again...Thanks for you advice.but I don't understand what you mean?do you mean? I should delete one destructor like no destrutor in derived class?I delete destrutor in derived class(CLoading class) but it still got the problem   ;Quote:Original post by fantasymeQuote:Original post by PsycrosisCleanUp is being called twice.... once in the inherited destructor and  in the base destructor... So the pointers are being deleted twice, hence the access violation... You can take it out of one of the destructor's or make the pointers null after you delete them... that way it won't matter if you call delete on them again...Thanks for you advice.but I don't understand what you mean?do you mean? I should delete one destructor like no destrutor in derived class?I delete destrutor in derived class(CLoading class) but it still got the problemInstead of:virtual void CleanUp(){delete p_tex; delete t_tex;}You should do:virtual void CleanUp(){delete p_tex; p_tex=NULL; delete t_tex; t_tex=NULL;}Otherwise you can end up calling delete p_tex when it's already been deleted - calling delete on a NULL pointer is perfectly valid, and will do nothing.   ;  Better yet, replace the raw pointers with smart pointers and don't worry about explicit clean up.   ;  When you have a virtual destructor in a base class it is called when the derived class is deleted or goes out of scope. When that happens the function CleanUp() is being called twice, hence pointers being deleted twice. What I am saying is the call to the CleanUp function only needs to be done in either the base or inherited destructor but not both. You should null out your pointers too as it is good coding practice. What you may have intended is a seperate cleanup for each destructor, for that you MUST override the CleanUp function in the inherited class or the base CleanUp will be called twice.Here's an example..class CCharacter{protected: CMakePolygon*p_tex;CLoadTextureClass*t_tex;intstatus;public:CCharacter(void);virtual ~CCharacter(void){CleanUp();}virtual void Init();virtual void Draw();virtual void Main();virtual void CleanUp(){delete p_tex; delete t_tex;}void SetStatus(int n){status = n;}};class CLoading: public CCharacter{private:intFPS;intround;public:CLoading(void);~CLoading(void){CleanUp();}void Init();void Main();void Animation();   // Add this!   virtual void CleanUp(){}};[Edited by - Psycrosis on May 28, 2010 8:05:10 AM];  Psycrosis: Truefantasyme: You also need to be very careful when calling virtual functions inside a destructor as this might not do what you expect it to do.   ;Quote:Original post by Evil SteveQuote:Original post by fantasymeQuote:Original post by PsycrosisCleanUp is being called twice.... once in the inherited destructor and  in the base destructor... So the pointers are being deleted twice, hence the access violation... You can take it out of one of the destructor's or make the pointers null after you delete them... that way it won't matter if you call delete on them again...Thanks for you advice.but I don't understand what you mean?do you mean? I should delete one destructor like no destrutor in derived class?I delete destrutor in derived class(CLoading class) but it still got the problemInstead of:virtual void CleanUp(){delete p_tex; delete t_tex;}You should do:virtual void CleanUp(){delete p_tex; p_tex=NULL; delete t_tex; t_tex=NULL;}Otherwise you can end up calling delete p_tex when it's already been deleted - calling delete on a NULL pointer is perfectly valid, and will do nothing.thanks for your help,but I still got error.Unhandled exception at 0x0018355e in RenewOracle.exe: 0xC0000005: Access violation reading location 0xcccccccc.I did as you advise.the problem is still delete p_tex;   ;Quote:Original post by fantasymebut I still got error.Unhandled exception at 0x0018355e in RenewOracle.exe: 0xC0000005: Access violation reading location 0xcccccccc.That means that the pointer was never initialised. In your constructor, you should be initialising all your pointers to NULL.Or, as SiCrane said - use a smart pointer class so you don't have to worry about this sort of thing.   ;  Or switch to a garbage collected language like (insert favorite .Net language here),Java or D and not worry about it lol.... Thats one thing I love about programming in C# is that Visual Studio warns you about not initializing something while you are coding...   