Re: check for deleted map entry -> crash ?
bolnvhuis@wanadoo.nl wrote:
I'm using an STL map in my code.
My application sometimes tries to delete things twice from the map.
This leads to a crash in my current code. The problem is probably the
way I check whether it is necessary to delete something (line 44 and 52
in below code).
In the below code the problem is demonstrated, line 65 will cause a
segmentation fault.
1 #include <iostream>
2 #include <cstdlib>
3 #include <map>
4
5 using namespace std;
6
7 class MyClass
8 {
9 public:
10 MyClass(int);
11 ~MyClass();
12 void show();
13 private:
14 int nr_;
15 };
16
17 MyClass::MyClass(int nr) : nr_(nr)
18 {
19 cout << "MyClass: " << nr_ << " constructed" << endl;
20 }
21
22 MyClass::~MyClass()
23 {
24 cout << "MyClass: " << nr_ << " destructed" << endl;
25 }
26
27 void MyClass::show()
28 {
29 cout << "Show this MyClass: " << nr_ << endl;
30 }
31
32 typedef map<int, MyClass *> RtdmCyclicTimerMap;
33
34 int main(void)
35 {
36 RtdmCyclicTimerMap cyclicRtdmTimer_;
37 map<int, MyClass *>::const_iterator cyclicRtdmIt;
I recommend against using raw dummy pointers in an STL container.
I recommend you use a smart pointer like the following:
http://axter.com/smartptr
The above smart pointer can be used with std::map, and will
automatically delete the pointee for you.