By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

template class & new

P: n/a
Hello,

I'm having a problem with a templated class. I have
two template classes Node and List. Everything works fine when
the type for List is an object and not a pointer
ie:"List<Object> theList". It works when it is a pointer as well
the problem is in a memory leak it causes. For example an example of
the problem it causes see the code below.

<snip>
List<Object *> myList;
Object *anObject = NULL;
....
anObject = new Object();
....
myList.Append(anObject);
....
</snip>

list.h
List<Type>::Append(Type &inputData)
{
Node<Type> *tmpNode = new Node<Type>(inputData);
....
Well it all works fine, except that in the destructor for the
Node object it doesn't know that it needs to delete the pointer
to the data. Nor does it know whether the pointer was allocated
via a call to new (though most likely it has...), and thus my
memory leak.

Any pointers (no pun intended) or links to information where I can
learn how to fix this? Is there a way to find out if my Node
class is of a pointer type, and better yet whether the data it
contains was allocated via a call to new?

--
Nathanael D. Noblet
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Nathanael D. Noblet" <na*******@nobletdesign.com> wrote in message
news:pa****************************@nobletdesign.c om...
Hello,

I'm having a problem with a templated class. I have
two template classes Node and List. Everything works fine when
the type for List is an object and not a pointer
ie:"List<Object> theList". It works when it is a pointer as well
the problem is in a memory leak it causes. For example an example of
the problem it causes see the code below.

<snip>
List<Object *> myList;
Object *anObject = NULL;
...
anObject = new Object();
...
myList.Append(anObject);
...
</snip>

list.h
List<Type>::Append(Type &inputData)
{
Node<Type> *tmpNode = new Node<Type>(inputData);
...
Well it all works fine, except that in the destructor for the
Node object it doesn't know that it needs to delete the pointer
to the data. Nor does it know whether the pointer was allocated
via a call to new (though most likely it has...), and thus my
memory leak.

Any pointers (no pun intended) or links to information where I can
learn how to fix this? Is there a way to find out if my Node
class is of a pointer type, and better yet whether the data it
contains was allocated via a call to new?

--
Nathanael D. Noblet


Go to

www.boost.org

and get yourself a shared_ptr. It works like this:

typedef boost::shared_ptr<Object> PObj;
List<PObj> myList;
myList.Append(PObj(new Object()));

Each PObj has its own destructor which takes care of memory management for
you.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #2

P: n/a
On Thu, 20 Nov 2003 04:19:05 +0000, Cy Edmunds wrote:
Go to

www.boost.org

and get yourself a shared_ptr. It works like this:

typedef boost::shared_ptr<Object> PObj;
List<PObj> myList;
myList.Append(PObj(new Object()));

Each PObj has its own destructor which takes care of memory management for
you.


OK I've looked a little there. I was also given a suggestion to look at
template specialization. I've started on that a bit, but am a bit short on
examples. I think the problem I'm experiencing has to do with the fact
that my specialization isn't for a specific data type, and that it is
whether the Type is a pointer to a specific data type or whether it is the
type itself. Anyone have suggestions to my problem using template
specialization of better yet good documentation on how to do it properly?
I'm looking through my only reference and it shows specialization for when
I specify a data type, but I want to only change it to <Type *> or
something like that... Ideas?

--
Nathanael D. Noblet
Jul 22 '05 #3

P: n/a
"Nathanael D. Noblet" <na*******@nobletdesign.com> wrote in message
news:pa****************************@nobletdesign.c om...
On Thu, 20 Nov 2003 04:19:05 +0000, Cy Edmunds wrote:
Go to

www.boost.org

and get yourself a shared_ptr. It works like this:

typedef boost::shared_ptr<Object> PObj;
List<PObj> myList;
myList.Append(PObj(new Object()));

Each PObj has its own destructor which takes care of memory management for you.


OK I've looked a little there. I was also given a suggestion to look at
template specialization. I've started on that a bit, but am a bit short on
examples. I think the problem I'm experiencing has to do with the fact
that my specialization isn't for a specific data type, and that it is
whether the Type is a pointer to a specific data type or whether it is the
type itself. Anyone have suggestions to my problem using template
specialization of better yet good documentation on how to do it properly?
I'm looking through my only reference and it shows specialization for when
I specify a data type, but I want to only change it to <Type *> or
something like that... Ideas?

--
Nathanael D. Noblet


Template specialization can never do this job. All the elements in a
standard container must be of the same type. With the smart pointer I gave
you, that rule is followed -- everything is a smart pointer to the same type
(the polymorphic base class). With templates, each template signature is a
different type and therefore cannot be put in the same container.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.