Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

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



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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.



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