Consistent New & Deletion

To continue my previous question , I would like to ask the following:

Given that the C ++ function has a new operator in it, but does not explicitly return anything (i.e., with the return operator), should it always have a delete too?

If not, could you give me an example.

+4
source share
7 answers

He does not need to explicitly return the newly created object, but he must somehow pass it on to something else. Possible scenarios:

  • In a member function, create an object and assign it to the field of the containing object.

Example:

class Foo { private: Baz* baz; public: Foo() : baz(0) { } void create_bar() { baz = new Baz(); } ~Foo() { delete baz; } }; 
  • Transferring a new object to something else.

Examples:

 void foo() { // assuming bar lives in the global scope bar.baz = new Baz(); } void foo2(Bar& bar) { bar.baz = new Baz(); // or, better: bar.setBaz(new Baz()); } 
  • Using some type of garbage collection pointer.

Example:

 std::auto_ptr<Baz> foo() { return new Baz(); } 
  • Passing a pointer to another function that does something with it, and then removes it.

Example:

 void foo(Bar* bar) { bar->dostuff(); delete bar; } void baz() { Bar* bar = new Bar(); foo(bar); } 
+4
source

The rule with the new is very simple: the pointer returned by new must be deleted at some point by someone, somewhere in the code. If the pointer returned by new is lost, forgotten or discarded, then you have a memory leak, and that is bad.

Therefore, if you new load some memory and return without saving it somewhere permanently, then your memory leaked. Therefore, if you are not going to delete it, then the pointer must either be stored in some kind of long-term storage (class member, static variable, etc.), or it must be returned so that someone else can delete it.

Alternatively, you can simply use boost :: shared_ptr and stop worrying about (most of) this.

+4
source

If a function creates a resource using new and does not pass a pointer to this resource for something, then yes, it must delete the resource, otherwise it will never be cleared, which will lead to a memory leak.

+1
source

Not if the purpose of this function is to instantiate some data and transfer it to another object. An example (hypothetical) from the gaming industry:

 void AddProjectileAtPoint(int x, int y) { Projectile *p = new Projectile(x, y); mProjectileManager->Add(p); //"mProjectileManager" job is to hold all projectiles and update them every frame... } 

In this case, the goal is clearly to create a new object representing some data and store it somewhere for later use.

Obviously, at some point it will be necessary to combine delete , but not inside the function where new occurs. This is fine as long as there is a well-defined procedure for transferring responsibility for managing new'd memory for some other component.

In this case, the structure is that the "projectile manager" takes responsibility for all the shells that are given to him, will keep them alive as long as necessary, and will clear his memory when appropriate.

+1
source

If you allocate memory with new in a function, and this memory is not available outside the function (or static), then it must be freed before the function exists. In this case, I would recommend using std::auto_ptr or boost::scoped_ptr , as it will call delete for you when the function finishes. Even if exceptions are thrown.

+1
source

Not necessary if it allocates a static object (for example, to implement the Singleton template).

0
source

No, not necessarily. You may have saved a pointer to an object with dynamic allocation somewhere else. for example for some class A :

 A* ptr = NULL; void foo() { ptr = new A(); } 

You should have delete ptr somewhere, but it should not be in foo() .

On the other hand, if you have not saved it anywhere:

 void foo() { A* ptr = new A(); } 

Then yes, it is a memory leak. You can never go back to this pointer to do delete ptr !


Please note that it is better to use some implementation of a smart pointer, such as shared_ptr or unique_ptr to process this material for you.

0
source

All Articles