I have read in a lot of books that for every "new" you must use a delete....But in some books I have been currently reading they don't bother deleting their pointer to a class.

I have read it is required at cplusplus.com to delete a pointer to a class but other books make no mention of it only standard type variables.

So is it required or not?

Here is an example if my post seems confusing:

is this ok?

void SomeClass::someMethod()
{
     AnotherClass *object = new AnotherClass();
     object->doSomething();
}

or do you have to do this?

void SomeClass::someMethod()
{
     AnotherClass *object = new AnotherClass();
     object->doSomething();
     delete object;
}

Recommended Answers

All 4 Replies

You have to do the second one.


EDIT: For memory leak diagnostics I recommend using Visual Leak Detector (if you're using Visual Studio)

EDIT 2: The reason you must delete, is that if we take your code as an example, you create an instance of the point "AnotherClass" and assign it some memory. You then do stuff with that class and the memory it points to. Then you exit the function and everything gets de-referenced. So you no longer have a pointer to your memory address that contains the "AnotherClass" class. This memory, however, is still assigned and in use (by your class) but you no longer have any method of getting to it. You have a "lost pointer".

Similarly if you re-assign the pointer before deleting it.

void SomeClass::someMethod()
{
     AnotherClass *object = new AnotherClass();
     object->doSomething();
     object = new AnotherClass();
     delete object;
}

You must use 'delete' on every 'new' variable. Thus the second is one to follow. It is a good practice to make your pointer to point to NULL after you have deleted the object to prevent your pointer pointing to unspecified location.

void SomeClass::someMethod()
{
     AnotherClass *object = new AnotherClass();
     object->doSomething();
     delete object;
     object = null;
}

This is useful when you want to reuse your pointer at a later stage.

You must use 'delete' on every 'new' variable. Thus the second is one to follow. It is a good practice to make your pointer to point to NULL after you have deleted the object to prevent your pointer pointing to unspecified location.

void SomeClass::someMethod()
{
     AnotherClass *object = new AnotherClass();
     object->doSomething();
     delete object;
     object = null;
}

This is useful when you want to reuse your pointer at a later stage.

Indeed, not assigning to NULL is commonly referred to as a dangling pointer and can cause all sorts of weird behaviour if you assume it's still assigned.

that unreged persons idea about a program that generates random mazes and then solves them is a great idea, it would be fun with the AI you'd have to make.

commented: Thanks for sharing -1
commented: and i'll be sure to NOT click your pathetic links. -2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.