template class & new

  • Thread starter Nathanael D. Noblet
  • Start date
N

Nathanael D. Noblet

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?
 
C

Cy Edmunds

Nathanael D. Noblet said:
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?

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.
 
N

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.

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?
 
C

Cy Edmunds

Nathanael D. Noblet said:
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?

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.
 

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

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,833
Members
47,380
Latest member
AlinaBlevi

Latest Threads

Top