user1096294
user1096294

Reputation: 849

C++ memory deallocation after return

I have this question regarding memory allocation and deallocation in c++. Here is the situation: I have a method foo which allocates memory and then returns that object:

Object foo () {
  Object *a = new Object();
  // Do something with this object...
  return *a;
}

and another method which uses this returned object:

void bar () {
  Object a = foo();
  // Do something..
}

And my question is, at which point should i deallocate the memory I have allocated? When i return from the method foo, does the method bar get a copy of that object on its stack, or does it get access to that one object somewhere in memory?

Thanks! Bart

Upvotes: 2

Views: 5428

Answers (4)

P3trus
P3trus

Reputation: 7215

Youre leaking memory because you are not returning the object you allocated, but a copy of it. The easiest solution is to not use new at all

Object foo () {
  Object a;
  return a;
}

If you really need the object on the heap, e.g. if it's a polymorphic object and your're returning a pointer to the base class it would look like

Base* foo () {
  Base *a = new Derived();
  return a;
}

But this code is far from good. It's not exception save which can lead to memory leaks as well. Therefore you should almost always wrap a new with a smart pointer like std::unique_ptr(only c++11), std::auto_ptr or boost::shared_ptr.

std::unique_ptr<Base> foo () {
  std::unique_ptr<Base> a(new Derived();
  return a;
}

Upvotes: 3

Benjamin Lindley
Benjamin Lindley

Reputation: 103693

You can't deallocate that object. It's lost. It's a memory leak. You never should have (dynamically) allocated it in the first place. Your code should have looked like this:

Object foo () {
  Object a;
  // Do something with this object...
  return a;
}

When i return from the method foo, does the method bar get a copy of that object on its stack, or does it get access to that one object somewhere in memory?

It's a copy of the still existing inaccessible object.

Upvotes: 6

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153792

Allocating memory with new tells the compiler: "don't worry, I know what I'm doing: this object will be controlled by me". That means, the compiler will, indeed, not worry about it and you are put in charge. The best solution is: don't do it unless you really know what you are doing. Note, that I'm pretty sure that I know what I'm doing and still I normally don't use new because there is no need!

In your example, you want to use

Object foo() {
    Object a;
    // do something
    return a;
}

Conceptually, the object is created on the stack and copied when it is returned. After the copy is done, the local object is destroyed. In practice, this is rarely what really happens: since the local object is destroyed right after it is copied, the compiler is given freedom to do what you normally want and that is: the object a isn't copied or destroyed but actually returned directly. However, this should yield the same result (if it does not, your class is broken somewhere).

Upvotes: 1

Chen Harel
Chen Harel

Reputation: 10052

In this example you can't. Since the caller function will never know where (or even aware) a dynamically object was created. If you were to use references (or pointers) then it would be the callers responsibility to release it when finished using it.

Upvotes: 0

Related Questions