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