anonymous pointer creation and memory leaks

D

drjackman

I have a quick question for you STL gurus out there. Suppose I do
something like this:

vector <myClass*> foo;
foo.push_back(new myClass());

Does this work in C++? I know you can do it in C# and Java. Also, if
you call foo.clear(), are the objects deleted properly (any memory
leaks doing this kind of thing)?

Thanks for your input!

DR
 
R

Rolf Magnus

I have a quick question for you STL gurus out there. Suppose I do
something like this:

vector <myClass*> foo;
foo.push_back(new myClass());

Does this work in C++?

If myClass is a type, yes. Why shouldn't it?
I know you can do it in C# and Java.

C# and Java don't have pointers.
Also, if you call foo.clear(), are the objects deleted properly (any
memory leaks doing this kind of thing)?

Depends on what you mean by "the objects". You store pointers in your
vector, and those pointers will be destroyed. The objects that they point
to, however, won't. How would the vector know whether you want them to be
deleted or not? You have to delete them yourself.
 
D

drjackman

You're right, C# and Java don't have pointers, but internally
everything is represented as dynamic references and are garbage
collected when the garbage collected gets around to it. Really,
besides the point.

But what you're saying is that if I have a vector and I call clear() to
clear them out (or if I want to remove one or several manually), I
should first delete any objects that are associated with those pointers
in that collection. Do I have it right?

DR
 
M

Malte Starostik

You're right, C# and Java don't have pointers, but internally
everything is represented as dynamic references and are garbage
collected when the garbage collected gets around to it. Really,
besides the point.

But what you're saying is that if I have a vector and I call clear() to
clear them out (or if I want to remove one or several manually), I
should first delete any objects that are associated with those pointers
in that collection. Do I have it right?

Yes. Or better yet avoid filling the vector with pointers unless you
have a good reason to do so. The main reasons that come to mind are
polymorhic or noncopyable/expensive to copy elements. In all of those
cases, smart pointers take the burden of deletion off you.

Cheers,
Malte
 
J

Jeff Flinn

Malte Starostik said:
Yes. Or better yet avoid filling the vector with pointers unless you
have a good reason to do so. The main reasons that come to mind are
polymorhic or noncopyable/expensive to copy elements. In all of those
cases, smart pointers take the burden of deletion off you.

To be explicit for C-Sharpies and Javites, when polymorphism is not
required:

struct myClass{};

void somefn()
{
std::vector<myClass> foo;

foo.push_back( myClass() );
}

All memory management is handled automatically.

Jeff Flinn
 
A

Anoop Aryal

But what you're saying is that if I have a vector and I call clear() to
clear them out (or if I want to remove one or several manually), I
should first delete any objects that are associated with those pointers
in that collection. Do I have it right?

the issue is of 'ownership'. the container doesn't know if you have the
pointer store elsewhere too (by the time you say foo.push_back(), the
pointer is already created as if you had done:

x* y = new x();
foo.push_back(y);

so your code could have easily been:
x* y = new x();
fooAnother.push_back(y);
foo.push_back(y);

therefore, foo doing a delete on the object that's pointed to would be
disastrous. to get a little closer to the behavior you get in java/C#
(although i don't know much about C# at all), look at boost::shared_ptr.
(http://www.boost.org/libs/smart_ptr/smart_ptr.htm)

be careful though, shared_ptr alone will get you in trouble if you have
circular pointers. (this used to be a problem in JVM gc until they
implemented mark-and-sweep way back, i think). to be
circular-reference-proof, you'd have to use a combination of shared_ptr and
weak_ptr. it's all discussed in the URL above.

cheers,
anoop aryal
 
D

drjackman

I appreciate all of your feedback! That makes sense and is probably
something that I should know already :) I'd have to say that those of
us who use Java/C# are spoiled to garbage collection (in all of it's
glory).

Thanks again all!

DR
 
P

Peter Koch Larsen

Rolf Magnus said:
If myClass is a type, yes. Why shouldn't it?

It does work, but there is a slight risc of a leak - namely if push_back
should throw.

[snip]


/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top