C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

delete, hash_map

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
SimpleCode
Guest





PostPosted: Fri May 04, 2007 10:57 pm    Post subject: delete, hash_map Reply with quote



class COsgCar;
class moving_vechicle;
hash_map<moving_vechicle*, COsgCar*> m_hash;

hash_map<moving_vechicle*, COsgCar*>::iterator iter;

iter = m_hash.find( pMovingVechicle );
if ( iter != m_hash.end() )
{
COsgCar *p = m_hash[pMovingVechicle];
delete p;
}
///////////////////////////////////////
It always run error.
I don't know why.
I just test for some days.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Daniel Krügler
Guest





PostPosted: Sat May 05, 2007 1:18 pm    Post subject: Re: delete, hash_map Reply with quote



On 5 Mai, 00:57, SimpleCode <DragonXL...@gmail.com> wrote:
Quote:
class COsgCar;
class moving_vechicle;
hash_map<moving_vechicle*, COsgCar*> m_hash;

hash_map<moving_vechicle*, COsgCar*>::iterator iter;

iter = m_hash.find( pMovingVechicle );
if ( iter != m_hash.end() )
{
COsgCar *p = m_hash[pMovingVechicle];
delete p;}

///////////////////////////////////////
It always run error.

But which one? Please provide the newsgroup with
all relevant information. A description like
"I have an error" is mostly the guarantee for little
response.

Quote:
I don't know why.
I just test for some days.

There are at least four candidates. In every case I
assume that pMovingVechicle is of type moving_vechicle*
or at least implicitly convertible to that type. I guess
that point (4) is the most probable reason for your
problem, but check also the other of the following
guys:

1) As written COsgCar is an incompletly defined type.
Invoking delete on such type can cause UB. Ensure
that this type is defined *before* deletion to fix this
issue.

2) Since I don't know the definition of COsgCar
you should consider the possibility that it might be
the base class of some derived type as dynamic type.
Ensure that in this situation the actual base type
(at least COsgCar) has a virtual destructor, otherwise
UB takes place, because you invalidly invoke delete
on a dynamic type different from its static type.

3) You call delete (the scalar version) on some
pointer, but it's not obvious from the context,
which form of new was used for its allocation.
- Ensure that the deleted item was indeed created
via the scalar new form (*not* new[] and not any
other form of storage, e.g. storage of static
or automatic duration).
- Ensure that this pointer has not been deleted
before.

4) Your are invoking delete on a contained pointer of
the container m_hash. After this action any read
access to this pointer causes UB. Because the
container still *contains* this pointer, it's quite probable
that the container attempts to read it. To prevent this
problem, you have to divide the deletion into several
parts:

(a) Save the current pointer into a temporary pointer.
(b) Remove this pointer from the map (e.g. via
erase or the proper removal function)
(c) Invoke delete on the temporary pointer and don't
read that pointer after this!

If all this does not help, you should give the
community more information concerning the error,
which might lead to the conclusion that you need
to provide us with more details.

Greetings from Bremen,

Daniel Krügler



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





PostPosted: Sat May 05, 2007 1:18 pm    Post subject: Re: delete, hash_map Reply with quote



SimpleCode <DragonXLong (AT) gmail (DOT) com> wrote:

Quote:
class COsgCar;
class moving_vechicle;
hash_map<moving_vechicle*, COsgCar*> m_hash;

hash_map<moving_vechicle*, COsgCar*>::iterator iter;

iter = m_hash.find( pMovingVechicle );
if ( iter != m_hash.end() )
{
COsgCar *p = m_hash[pMovingVechicle];
delete p;
}
///////////////////////////////////////
It always run error.
I don't know why.
I just test for some days.
I don't know the inards of your hash_map but I might guees, that it

holds a pair<moving_vechicle *,COsqCar *> and it is deleting it twice.
Perhaps something like:

iter = m_hash.find(pMovinfVechicle);
if(iter != m_hash.end())
{
moving_vechicle *pv = iter->first;
COsqCar * pc = iter->second;
m.hash.erase(iter);
delete pv;
delete pc;
}

assuming that a pair<const key_type,mapped_type> & is what *iterator
returns.

if hash_map is old enough you might need (*iter)....
instead of iter->...


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





PostPosted: Sat May 05, 2007 9:21 pm    Post subject: Re: delete, hash_map Reply with quote

SimpleCode <DragonXLong (AT) gmail (DOT) com> writes:

Quote:
class COsgCar;
class moving_vechicle;
hash_map<moving_vechicle*, COsgCar*> m_hash;

hash_map<moving_vechicle*, COsgCar*>::iterator iter;

iter = m_hash.find( pMovingVechicle );
if ( iter != m_hash.end() )
{
COsgCar *p = m_hash[pMovingVechicle];
delete p;
}
///////////////////////////////////////
It always run error.

Please elaborate. What kind of error?


Quote:
I don't know why.

Well, you are leaving a pointer in the map that you are not even
allowed to read (let alone dereference). This can cause all kinds of
trouble in the code you don't show us (technically also in the code
you do show us, but it's not very likely).

Did you mean to also remove the element from the hash_map?


Side note: It seems that your hash_map owns the COsgCar objects that
its elements refer to. If that's the case, consider chaing the
m_hash's type to something like

hash_map<moving_vechicle*, boost::shared_ptr<COsgCar> > m_hash;

and you won't have to explictly delete the COsgCar objects after
removing the hash_map elements that refer to them.

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





PostPosted: Sat May 05, 2007 9:27 pm    Post subject: Re: delete, hash_map Reply with quote

Quote:
But which one? Please provide the newsgroup with
all relevant information. A description like
"I have an error" is mostly the guarantee for little
response.

Thanks, I will improve my manners of putting problems.

Quote:
There are at least four candidates. In every case I
assume that pMovingVechicle is of type moving_vechicle*

Yes, It's the fact.

Quote:
or at least implicitly convertible to that type. I guess
that point (4) is the most probable reason for your
problem, but check also the other of the following
guys:

When I read point(4), I follow it and run after my program.
At last, I found it, as point(4).
My program depends on the OpenSceneGraph(OSG) library which packed the
OpenGL.And this is my first time to use OSG.Every COsgCar's object
must be added to the OSG Viewer's Node(rootNode).
The problem happened, because I delete the object before I remove the
object from the rootNode. This is the key.

Quote:
1) As written COsgCar is an incompletly defined type.
Invoking delete on such type can cause UB. Ensure
that this type is defined *before* deletion to fix this
issue.

By the way, what's UB?

Quote:

2) Since I don't know the definition of COsgCar
you should consider the possibility that it might be
the base class of some derived type as dynamic type.
Ensure that in this situation the actual base type
(at least COsgCar) has a virtual destructor, otherwise
UB takes place, because you invalidly invoke delete
on a dynamic type different from its static type.

3) You call delete (the scalar version) on some
pointer, but it's not obvious from the context,
which form of new was used for its allocation.
- Ensure that the deleted item was indeed created
via the scalar new form (*not* new[] and not any
other form of storage, e.g. storage of static
or automatic duration).
- Ensure that this pointer has not been deleted
before.

4) Your are invoking delete on a contained pointer of
the container m_hash. After this action any read
access to this pointer causes UB. Because the
container still *contains* this pointer, it's quite probable
that the container attempts to read it. To prevent this
problem, you have to divide the deletion into several
parts:

(a) Save the current pointer into a temporary pointer.
(b) Remove this pointer from the map (e.g. via
erase or the proper removal function)
(c) Invoke delete on the temporary pointer and don't
read that pointer after this!

If all this does not help, you should give the
community more information concerning the error,
which might lead to the conclusion that you need
to provide us with more details.

Friend, I think you are a seasoned superior.
Thanks for your help.
You taught me how to find out the problem's solution.Not just the
surface^-^


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.