Re: Desctruction of objects inside a static nested map holding
pointers
jayaramganapa...@gmail.com wrote:
On Nov 24, 5:24 pm, James Kanze <james.ka...@gmail.com> wrote:
On Nov 23, 3:19 pm, jayaramganapa...@gmail.com wrote:
The C++ class do a new and delete frequently. But the C++ hold the my
MAP as static variable.
First I tried without pointers. Due to some issues of copy
constructors of nested map entries I beleive, I was not hitting the
cache properly.
I'm not sure what you mean here, but there is a requirement that
the map types be copiable. If the identity of the objects in
the map is important, then you need pointers. But normally, the
identity of an std::map is not important. And std::map has an
appropriate copy constructor. So it shouldn't be a problem.
So I changed to pointers and printed the pointer. So
everything went fine. But I ended up creating memory leaks.
What do you mean by memory leaks?
But my hope was since the map is a container there may be some
logic like, The map's destructor getting called. So map has to
clean up. If the maps key or value is a pointer then it
invokes a delete on the pointer. I have used only new. No
malloc. So I guess delete operator should be fine if the map
holds a pointer. Is C++/STL smart enough to do that? So gave a
try.
The possible solution I can think of is
1) If I can override map destrutor then I can do my stuff of cleaing
and then the standard map cleaup.( This is a dream)
2) inherit the map and create my own map and crete its staic objevt
inside my class.
3) Create a wrapper clas against map and make it static and in its
destructor clean up the map
4) Do additional code for a geniric singleton pattern for solution two
or three and use it happily in future.
What is C++ gurus advice?
My recommendations are still 1) make it work without pointers,
and 2) don't worry about the "leaks". If you don't remove
objects from the map other than at the end of the program, it
doesn't leak memory.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34