471,082 Members | 706 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,082 software developers and data experts.

delete, hash_map

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.

May 5 '07 #1
6 5994
"SimpleCode" <Dr*********@gmail.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
: 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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com

May 5 '07 #2

Ivan Vecerina д
"SimpleCode" <Dr*********@gmail.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
: 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.

May 5 '07 #3
"SimpleCode" <Dr*********@gmail.comwrote:
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.
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form

May 5 '07 #4
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.
----------------
Regards Lung

May 5 '07 #5
"SimpleCode" <Dr*********@gmail.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
: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.
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form

May 6 '07 #6
On May 5, 5:03 am, SimpleCode <DragonXL...@gmail.comwrote:
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.
----------------
Regards Lung
what exactly is the error message? A double free exception? a memory
corrupted exception? What is the destructor of COsgCar look like?

Regards,

PQ

May 6 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Florian Liefers | last post: by
5 posts views Thread by peter_k | last post: by
1 post views Thread by jayesah | last post: by
4 posts views Thread by newbie | last post: by
2 posts views Thread by Amit Bhatia | last post: by
5 posts views Thread by frankw | last post: by
2 posts views Thread by marek.vondrak | last post: by

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.