From: Sideswipe on
Say I have a map<key,MyObject*> myMap;

and I add to it:

myMap[1] = new MyObjectA(..);
myMap[2] = new MyObjectB(..);
myMap[3] = new MyObjectC(..);

presume that 'MyObjectX' is a subclass of MyObject -- and and now I do:

map<key,MyObject*>::iterator itr;
for(itr = objects.begin(); itr != objects.end(); ++itr){
delete itr->second;
}
Will this delete everything properly or is it going to delete based on
the pointer from the virtual table? (and thus dangling and or
corrupting memory)

Christian

http://christian.bongiorno.org


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Salt_Peter on

Sideswipe wrote:
> Say I have a map<key,MyObject*> myMap;
>
> and I add to it:
>
> myMap[1] = new MyObjectA(..);
> myMap[2] = new MyObjectB(..);
> myMap[3] = new MyObjectC(..);
>
> presume that 'MyObjectX' is a subclass of MyObject -- and and now I do:
>
> map<key,MyObject*>::iterator itr;
> for(itr = objects.begin(); itr != objects.end(); ++itr){
> delete itr->second;
> }
> Will this delete everything properly or is it going to delete based on
> the pointer from the virtual table? (and thus dangling and or
> corrupting memory)
>
> Christian

Have you tried it? Why rely on what anyone says rather than proof?

#include <iostream>
#include <map>
#include <boost/shared_ptr.hpp>

struct Object
{
virtual ~Object() { std::cout << "~Object\n"; }
};

class ObjectX : public Object
{
public:
ObjectX() { std::cout << "ObjectX\n"; }
~ObjectX() { std::cout << "~ObjectX\n"; }
};

int main()
{
typedef boost::shared_ptr< Object > SharedPtrObj;
typedef std::map< int, SharedPtrObj > MType;
MType mob;

// allocate 5
for(int i = 0; i < 5; ++i)
{
boost::shared_ptr< ObjectX > sp_objx( new ObjectX );
mob.insert( std::make_pair(i, sp_objx) );
}

// show them
typedef MType::iterator MIter;
for(MIter miter = mob.begin(); miter!= mob.end(); ++miter)
{
std::cout << "key: " << (*miter).first;
std::cout << "\tp_objx: " << (*miter).second;
std::cout << std::endl;
}
}


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Gerhard Menzl on
Sideswipe wrote:

> Say I have a map<key,MyObject*> myMap;
>
> and I add to it:
>
> myMap[1] = new MyObjectA(..);
> myMap[2] = new MyObjectB(..);
> myMap[3] = new MyObjectC(..);
>
> presume that 'MyObjectX' is a subclass of MyObject -- and and now I
> do:
>
> map<key,MyObject*>::iterator itr;
> for(itr = objects.begin(); itr != objects.end(); ++itr){
> delete itr->second;
> }
> Will this delete everything properly or is it going to delete based on
> the pointer from the virtual table? (and thus dangling and or
> corrupting memory)

Assuming that you meant to type "myMap" where you typed "objects", this
will delete every object derived from MyObject, providing MyObject has a
public virtual destructor. It will, however, leave dangling pointers
(i.e. pointers pointing to objects that are deceased, bereft of life,
gone to meet their maker etc.) in the map.

--
Gerhard Menzl

Non-spammers may respond to my email address, which is composed of my
full name, separated by a dot, followed by at, followed by "fwz",
followed by a dot, followed by "aero".



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: James Kanze on
Salt_Peter wrote:

[...]
> Have you tried it? Why rely on what anyone says rather than proof?

Perhaps because trying it only tells you what one particular
compiler happens to do in its current version, not what is
guaranteed to work. The standard library is just loaded with
ways to encounter undefined behavior, many of which happen to
work with some compilers and some implementations of the
library. As it happens, what he is doing is guaranteed, but
just trying it with one particular compiler (or even a number of
different compilers) won't tell you that.

--
James Kanze (GABI Software) email:james.kanze(a)gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Sideswipe on
That's good to know. I am actually having a problem with this situation
which is why I asked and I wasn't sure if the Polymorphism would affect
it.

MyObject is an interface and has all pure virtuals (it does not
specifiy a virtual destructor though and that would be my problem
(doesn't it need to be a 'pure' virtual dest?).

Basically, my memory profiler/checker is telling me that the mentioned
loop is deleting memory blocks NOT at the begginning of the block. I
access these pointer as objects pointers and don't mess their addresses
at all (why would I !?). I am just trying to figure out why it's
complaining.

But, you mentioned I would still have dangling pointers. To what?! I
freely admit to not having a rock-solid grasp on C++ and appreciate all
input.

Christian
http://christian.bongiorno.org


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]