delete, hash_map

S

SimpleCode

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

Ivan Vecerina

: 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;

Deleting the pointer will not remove it from the hash_map,
so you are leaving in the hash-map a pointer to freed memory.
Try adding, for example:
m_hash.erase(pMovingVehicle); // remove the hash_map entry

: }
:
:
: ///////////////////////////////////////
: It always run error.
: I don't know why.
: I just test for some days.

hth -Ivan
 
S

SimpleCode

Ivan Vecerina дµÀ£º
: 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;

Deleting the pointer will not remove it from the hash_map,
so you are leaving in the hash-map a pointer to freed memory.
Try adding, for example:
m_hash.erase(pMovingVehicle); // remove the hash_map entry

: }
:
:
: ///////////////////////////////////////
: It always run error.
: I don't know why.
: I just test for some days.

hth -Ivan

yes, I also use it.
The most important, I can't delete the COsgCar object which contains
many memory.
And I don't why I can't do it.
 
I

Ivan Vecerina

SimpleCode said:
The most important, I can't delete the COsgCar object which contains
many memory.
And I don't why I can't do it.

Well, either the object has already been deleted,
or its destructor does an illegal operation (e.g.
maybe a recursion problem?).
It is impossible to tell from the code you've posted.
 
S

SimpleCode

Well, either the object has already been deleted,
or its destructor does an illegal operation (e.g.
maybe a recursion problem?).
It is impossible to tell from the code you've posted.

My code:
#include <hash_map>
stdext::hash_map<moving_vehicle*, COsgCar*> g_hash; //global
variable
typedef pair<moving_vehicle*, COsgCar*> Ptr_Pair;

void COsgView::setRootChild(float x, float y, float z, float angle,
int type, moving_vehicle* pMovingVehicle)
{
/**** new, only hear ****/
COsgCar * osgcar = new COsgCar(x, y, z, angle, type);
g_hash.insert(Ptr_Pair(pMovingVehicle, osgcar));
}

vehicle_node* car_list::delete_car(vehicle_node *k)
{
stdext::hash_map<moving_vehicle*, COsgCar*>::iterator hash_Iter;
hash_Iter = g_hash.find(k->vehicle_p);
if (hash_Iter != g_hash.end())
{
COsgCar *p = g_hash[k->vehicle_p];
g_hash.erase(hash_Iter);
delete p; /**** If I comment, there is no problem ***/
p = NULL;
}
}

A puzzle.
 
I

Ivan Vecerina

:> Well, either the object has already been deleted,
: > or its destructor does an illegal operation (e.g.
: > maybe a recursion problem?).
: > It is impossible to tell from the code you've posted.
:
: My code:
: #include <hash_map>
: stdext::hash_map<moving_vehicle*, COsgCar*> g_hash; //global
: variable
: typedef pair<moving_vehicle*, COsgCar*> Ptr_Pair;
:
: void COsgView::setRootChild(float x, float y, float z, float angle,
: int type, moving_vehicle* pMovingVehicle)
: {
: /**** new, only hear ****/
: COsgCar * osgcar = new COsgCar(x, y, z, angle, type);
: g_hash.insert(Ptr_Pair(pMovingVehicle, osgcar));
: }
:
: vehicle_node* car_list::delete_car(vehicle_node *k)
: {
: stdext::hash_map<moving_vehicle*, COsgCar*>::iterator hash_Iter;
: hash_Iter = g_hash.find(k->vehicle_p);
: if (hash_Iter != g_hash.end())
: {
: COsgCar *p = g_hash[k->vehicle_p];
: g_hash.erase(hash_Iter);
: delete p; /**** If I comment, there is no problem ***/
: p = NULL;
: }
: }
:
: A puzzle.

Well, it's one of two things:
- the pointer that you are deleting is invalid
You could try to log object addresses in the constructor
and destructor to find out.
- the destructor of COsgCar does something wrong...

Again, the code you've posted seems ok.
 
P

pmouse

Well, either the object has already been deleted,
or its destructor does an illegal operation (e.g.
maybe a recursion problem?).
It is impossible to tell from the code you've posted.

My code:
#include <hash_map>
stdext::hash_map<moving_vehicle*, COsgCar*> g_hash; //global
variable
typedef pair<moving_vehicle*, COsgCar*> Ptr_Pair;

void COsgView::setRootChild(float x, float y, float z, float angle,
int type, moving_vehicle* pMovingVehicle)
{
/**** new, only hear ****/
COsgCar * osgcar = new COsgCar(x, y, z, angle, type);
g_hash.insert(Ptr_Pair(pMovingVehicle, osgcar));

}

vehicle_node* car_list::delete_car(vehicle_node *k)
{
stdext::hash_map<moving_vehicle*, COsgCar*>::iterator hash_Iter;
hash_Iter = g_hash.find(k->vehicle_p);
if (hash_Iter != g_hash.end())
{
COsgCar *p = g_hash[k->vehicle_p];
g_hash.erase(hash_Iter);
delete p; /**** If I comment, there is no problem ***/
p = NULL;
}

}

A puzzle.

what exactly is the error message? A double free exception? a memory
corrupted exception? What is the destructor of COsgCar look like?

Regards,

PQ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,591
Members
45,103
Latest member
VinaykumarnNevatia
Top