Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++
Expertise: All
Aug 14, 1998

Avoid Deleting a Pointer More Than Once

The results of deleting an object more than once are undefined. However, if code modifications are impossible (when a third party code is used, for example), a temporary workaround to this bug is assigning a NULL value to a pointer right after it has been deleted. It is guaranteed that a NULL pointer deletion is harmless.
 
String * ps = new String;
//...use ps
if ( TrueCondition ) {
		delete ps; 
	ps = NULL; //safety-guard: further deletions of ps will be harmless
}
//...many lines of code
delete ps; //a bug. ps is deleted for the second time. However, it's harmless
Please note that this hack is not meant to replace a thorough code review and debugging; it should be used as a transitory band-aid.
Danny Kalev
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date