473,803 Members | 3,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

delete, hash_map

class COsgCar;
class moving_vechicle ;
hash_map<moving _vechicle*, COsgCar*m_hash;

hash_map<moving _vechicle*, COsgCar*>::iter ator 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 6164
"SimpleCode " <Dr*********@gm ail.comwrote in message
news:11******** **************@ e65g2000hsc.goo glegroups.com.. .
: class COsgCar;
: class moving_vechicle ;
: hash_map<moving _vechicle*, COsgCar*m_hash;
:
: hash_map<moving _vechicle*, COsgCar*>::iter ator 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(pM ovingVehicle); // 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*********@gm ail.comwrote in message
news:11******** **************@ e65g2000hsc.goo glegroups.com.. .
: class COsgCar;
: class moving_vechicle ;
: hash_map<moving _vechicle*, COsgCar*m_hash;
:
: hash_map<moving _vechicle*, COsgCar*>::iter ator 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(pM ovingVehicle); // 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*********@gm ail.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_ma p<moving_vehicl e*, COsgCar*g_hash; //global
variable
typedef pair<moving_veh icle*, COsgCar*Ptr_Pai r;

void COsgView::setRo otChild(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(P tr_Pair(pMoving Vehicle, osgcar));
}

vehicle_node* car_list::delet e_car(vehicle_n ode *k)
{
stdext::hash_ma p<moving_vehicl e*, COsgCar*>::iter ator 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(ha sh_Iter);
delete p; /**** If I comment, there is no problem ***/
p = NULL;
}
}

A puzzle.
----------------
Regards Lung

May 5 '07 #5
"SimpleCode " <Dr*********@gm ail.comwrote in message
news:11******** **************@ q75g2000hsh.goo glegroups.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_ma p<moving_vehicl e*, COsgCar*g_hash; //global
: variable
: typedef pair<moving_veh icle*, COsgCar*Ptr_Pai r;
:
: void COsgView::setRo otChild(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(P tr_Pair(pMoving Vehicle, osgcar));
: }
:
: vehicle_node* car_list::delet e_car(vehicle_n ode *k)
: {
: stdext::hash_ma p<moving_vehicl e*, COsgCar*>::iter ator 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(ha sh_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...@gm ail.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_ma p<moving_vehicl e*, COsgCar*g_hash; //global
variable
typedef pair<moving_veh icle*, COsgCar*Ptr_Pai r;

void COsgView::setRo otChild(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(P tr_Pair(pMoving Vehicle, osgcar));

}

vehicle_node* car_list::delet e_car(vehicle_n ode *k)
{
stdext::hash_ma p<moving_vehicl e*, COsgCar*>::iter ator 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(ha sh_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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
9988
by: Florian Liefers | last post by:
"Hello World\n", i get error C2143 (Syntaxerror, missing ';' before '<') using the following code: #include <hash_map> struct eqstr { bool operator()(const char* s1, const char* s2) const
4
2173
by: Joel | last post by:
I have this bug that quite puzzled me. Basically I am having a segmentation fault on deleting an object, which belongs to a class which is the result of multiple inheritance from two other classes. None of the classes actually allocate memory on the heap. I simplified my code into one piece to show below. I will really appreciate if someone can tell me where the problem is. I have spent quite some time on it and felt a bit lost at this...
5
8625
by: peter_k | last post by:
Hi I've defined hash_map in my code using this: ------------------------------------------- #include <string> #include <hash_map.h> & namespace __gnu_cxx {
3
3844
by: kony | last post by:
Hi there, I would much appreciate your help with the following problem. Below is the code that uses a hash_map. I want to release all the memory occupied by the hash_map for other use. Apparently clear() function is not working and the trick with swap() is half working. Does anybody know how to deallocate the hash_map? Thanks in advance. Kon #include <functional>
1
9404
by: jayesah | last post by:
Hi All, I am developing my code with Apache stdcxx. I am bound to use STL of Apache only. Now today I need hash_map in code but as I learned, it is not available in Apache since it is not standard c++. Though it is available with GNU STL. The code module where I use hash_map will generate separate object file during compilation. This code module is also using STL string.
4
3215
by: newbie | last post by:
For STL containers like hash_map, need I explicitly call clear() before delete()ing it? e.g. hash_map<string, MyClass**my_map; my_map = new (hash_map<string, MyClass*>); .... ....
2
4284
by: Amit Bhatia | last post by:
Hi, I am trying to use hash maps from STL on gcc 3.3 as follows: #ifndef NODE_H #define NODE_H #include <ext/hash_map> #include "node_hasher.h" class Node; typedef hash_map<pair<int,int>, Node, Node_HasherLoc_Tree;
5
3376
by: frankw | last post by:
Hi, I have a hash_map with string as key and an object pointer as value. the object is like class{ public: float a; float b; ...
2
8152
by: marek.vondrak | last post by:
Hi, I am wondering if there are any functional differences between SGI's hash_map and tr1's unordered_map. Can these two containers be interchanged? What would it take to switch from hash_map to unordered_map? Thank you. -Marek
0
9700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10546
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10292
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10068
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9121
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7603
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.