Will this lead to memory leak in C ++?

I have a memory management issue in C ++ that is (obviously) related to references and pointers. Suppose I have a Class class with the my_method method:

 OtherClass& Class::my_method( ... ) { OtherClass* other_object = new OtherClass( ... ); return *other_object; } 

Meanwhile, in the nearest code snippet:

 { Class m( ... ); OtherClass n; n = m.my_method( ... ); } 

So, I know that there is a general rule about pointers (~ "something new should be delete-d") to avoid memory leaks. But basically, I take a reference to my object allocated for the heap, so when n goes out of scope, you should not call the OtherClass destructor, freeing the memory previously indicated by the other_object? Therefore, the real question ultimately arises: will this lead to a memory leak?

Thanks.

+6
c ++ memory-management pointers reference memory-leaks
source share
7 answers

It's pretty obvious that you want to return a new object to the caller, which does not need to be referenced. For this purpose, it is easiest to return an object by value.

 OtherClass Class::my_method( ... ) { return OtherClass( ... ); } 

Then in the calling code you can build a new object as follows.

 { Class m( ... ); OtherClass n( m.mymethod( ... ) ); } 

This avoids any worries about returning links to temporary requests or requiring the client to remove the administrator of the returned pointer. Please note that this requires that your object be copied, but this is a legitimate and usually implemented optimization for a copy that should be avoided when returning by value.

You will only need to consider a generic pointer or similar if you require joint ownership or for the object to have a lifetime outside the scope of the calling function. In this latter case, you can leave this decision to the client and still return by value.

eg.

 { Class m( ... ); // Trust me I know what I'm doing, I'll delete this object later... OtherClass* n = new OtherClass( m.mymethod( ... ) ); } 
+6
source share

Yes, which will lead to a memory leak.

What you will do, in the return statement, search for the new object that you created. The compiler will invoke the assignment operator as part of the return value and copy the CONTENTS of your new object to the object to which it was assigned in the calling method.

The new object will be left on the heap, and its pointer will be cleared from the stack, thereby creating a memory leak.

Why not return the pointer and manage it that way?

+12
source share

Calling a destructor and freeing memory are two different things in C ++.

delete calls the destructor and frees memory. delete[] calls the destructor for the allocated number of elements, then frees memory.

When OtherClass goes out of scope, the destructor is called, but memory is not freed.


As an assumption, when you feel that you have fully understood C ++ pointers, look at smart pointers, for example. enhance smart pointers to simplify memory management in C ++. (e.g. see article here for review)
+5
source share

You have 2 OtherClass objects:

One of them is n, which is created on the stack and is successfully deleted when it leaves the scope.

The other is the one you create on the heap inside my_method. This one is never deleted, and this will lead to a memory leak.

+2
source share

Usually, when a local object goes out of scope, it is freed only because it is allocated on the stack and the stack is automatically cleared. Since your object is allocated on the heap, it cannot be automatically freed. The only way to free it is to explicitly call delete.

I think you can probably do this:

Declare another DummyClass class that contains an open element that is a pointer to a OtherClass object. In the DummyClass constructor, clear the pointer to NULL. In your function, declare an object of type DummyClass and its member pointer to create another object of type OtherClass . Then, in the DummyClass destructor DummyClass check to see if the pointer is NULL, if not, delete it. This way your object will be cleaned up automatically when the DummyClass object goes beyond.

+2
source share

If possible, you can consider std :: auto_ptr or boost / c0x shared_ptr to simplify memory management.

+2
source share

If you insist on stack distribution, do not use the new operator in my_method() and pass a reference to n , namely:

 void Class::my_method( OtherClass& other_object, ... ) { other_object.init( ... ); } 

Then call my_method() as follows:

 { Class m( ... ); OtherClass n; m.my_method( n, ... ); } 

For this template to work, Class must implement some kind of init() method, which allows you to properly initialize objects without calling the constructor.

+2
source share

All Articles