View Full Version : Can I pass a NEW object into a function by-ref & without a delete? [C++]

06-20-2009, 07:49 AM
I want to pass an object by reference into a function (of another class), can this be done without causing a memory leak or requiring me to perform a delete?

Allow me to illustrate what I mean ...

class A
vector<B*> v; // vector of pointers to Class B objects

void Add(B &b) // function to add pointers to vector
~A {... delete everything from the vector ...}

class B
... some stuff ...

void main()
A *a; // pointer to instance of A
a = new A();
a->Add(new B());

So, the question is for the following line in particular:
a->Add(new B());
In this case I create a new Instance of B() which is passed (by reference) to the A.Add() function ... Can I do this or will it cause a memory leak? Also will the vector in A() have a correct pointer to the object created and passed in?

My goal is to simply pass the instance to Class A without making the vector public and accessing it directly...

Let me know if that makes any sense...

06-20-2009, 08:11 AM
If I make the following code-changes it compiles and seems to work ... but is there a memory leak due to my "NEW" inside of MAIN?

void Add(B *b) // function to add pointers to vector

06-20-2009, 08:35 AM
Yes there would be a memory leak. You'd need to delete 'a' before the end of the program. If your class goes through the vector and calls delete on the pointers, then you can call new the way you are doing without leaking memory. Albeit calling new as a function parameter is a little messy in my opinion.

06-20-2009, 09:57 AM
I do delete A when I am done already (in main), my worry is more the NEW in the function parameter ... will that cause any issues?

ralph l mayo
06-21-2009, 04:02 PM
As oracleguy said, the use of 'new' will cause an issue, memory leaks in the pointers to dynamically-allocated instances of B.

A container (like vector) of raw pointers cannot safely deallocate its contents when it is destroyed because it doesn't know if some other part of the program contains pointers to the resource that it might still use.

Two solutions:

1. use boost::shared_ptr (http://www.boost.org/doc/libs/1_39_0/libs/smart_ptr/shared_ptr.htm).

You pass the shared pointer a raw pointer, and it keeps track of how many things in your program are using it ("reference-counting"). For example, adding it pointer to a vector would increase the reference count by one, and the destruction of that vector would then decrease it by one. When the count reaches zero, the shared pointer deletes the raw pointer. This makes memory leaks difficult to achieve except in cases of objects that contain nested references to themselves, in which case see the linked docs.

The big caveat about this method is that, if it matters, you'll need to think carefully about which parts of the program hold a reference to the pointer to make sure that it goes away at the appropriate time.

Depending on how you add it to the example code you posted it could trigger deletions either at the vector's destruction or at the end of main. Deleting things that are still allocated at the end of main is a good idea, but it doesn't really help much. At that point the OS is going to step in and reclaim memory whether you still have a pointer to it or not.

2. Iterate the vector and delete its contents prior to its destruction. In ~A do something like:

for (std::vector<B*>::iterator it = v.begin(); it != v.end(); ++it)
delete *it;

Which has the advantage of being unambiguous about exactly which point in the program the memory is going to be deallocated. The disadvantage is that you're assuming the vector is the only reference to that pointer, which may not be the case if someone else is working with your code or if you forget about the implicit contract you're in to sign over the management of that pointer to the class for the remainder of the execution time.

it career
06-22-2009, 08:49 AM
You can use a smart pointer to take care of memory leak.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum