473,804 Members | 3,091 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template class & new

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(a nObject);
....
</snip>

list.h
List<Type>::App end(Type &inputData)
{
Node<Type> *tmpNode = new Node<Type>(inpu tData);
....
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
3 3975
"Nathanael D. Noblet" <na*******@nobl etdesign.com> wrote in message
news:pa******** *************** *****@nobletdes ign.com...
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(a nObject);
...
</snip>

list.h
List<Type>::App end(Type &inputData)
{
Node<Type> *tmpNode = new Node<Type>(inpu tData);
...
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_p tr<Object> PObj;
List<PObj> myList;
myList.Append(P Obj(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
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_p tr<Object> PObj;
List<PObj> myList;
myList.Append(P Obj(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
"Nathanael D. Noblet" <na*******@nobl etdesign.com> wrote in message
news:pa******** *************** *****@nobletdes ign.com...
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_p tr<Object> PObj;
List<PObj> myList;
myList.Append(P Obj(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2491
by: Frank Schmitt | last post by:
Hi! Suppose I have a class template Foo with a member function template bar - so far so good: template <typename T> struct Foo { template <typename U> void bar(const U& u); };
6
1776
by: NKOBAYE027 | last post by:
FIRST POST Hi All: I'm trying to write a simple specialization before moving on to something a bit more complex - always a good idea in my case, at least. :o) I'm trying to adapt the example from Stroustrup, 3rd ed., The C++ Programming Language p. 344 I'm using MSDev 6.0 in case that's an issue. Here's the source...
1
1304
by: | last post by:
I think, a class template with a static vector member: ---------------------------- template <class C> struct Shared { unsigned int count; C data; }; template <class C>
3
2675
by: Thomas Matthews | last post by:
Hi, I would like to apply inheritance to a template parameter, but my design fails to compile: cannot initialize one template class with child child parameterized class. I'll explain... Given: #include <string> // Parent class
3
7806
by: Cheng Mo | last post by:
When overriding operator new & delte of one class, the method is implicitly declared as static. However, overriding operator new & delete of template cannot be static.The compiler says cannot declare the function a static linkage. Why? C++ is so complex and so many situation should be considered.
5
1826
by: krema2ren | last post by:
Hi Does some of you know how to declare getCollection() in the cpp file? My code snippet below produces following compile error: foo.cpp(48) : error C2143: syntax error : missing ';' before '&' foo.cpp(48) : error C2501: foo<T>::Collection' : missing storage-class or type specifiers foo.cpp(48) : error C2065: 'T' : undeclared identifier foo.cpp(48) : error C2955: 'foo' : use of class template requires
2
1853
by: Glenn G. Chappell | last post by:
I am trying to write two constructors for the same class. One takes an iterator and so is a template. The other takes a particular type by reference to const. class Foo { public: template<typename InputIterator> Foo(InputIterator i); Foo(const Bar & b);
9
2788
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second assign() function really a specialization of the first assign() or is it an assign() overload? Thank you. -- Marek
8
1980
by: mast2as | last post by:
Hi guys, I think from what I found on the net that the code is correct and there's no problem when I compile the files. The problems occurs when I link them. I have a friend function which outputs points data to the ostream. Here is the error message: objs/generalpolygons.o(.text+0xf08): In function `GeneralPolygons::readObjFile(std::basic_string<char, std::char_traits<char>, std::allocator<char)': core/generalpolygons.cpp:222:...
1
4469
by: matz2k | last post by:
I've got a big problem with the CSS layout which I've produced with Photoshop/Dreamweaver especially for my ebay auctions. This is what it looks like http://cgi.ebay.de/ws/eBayISAPI.dll?ViewItem&item=250542673235 As you can see it can only be seen partially , can anyone help me with this? As fas as I can tell ebay's Iframe is messing up my layput, or am I wrong? Thx! here is the css/html code
0
9569
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10558
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9130
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2975
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.