deleting a pointer crashes the program.

Hi, Im having some trouble with deleting pointers.
The pointer which Im having trouble with is the one called: MyString* player_ptr. It seems to me that the pointer is pointing to some trash in memory when I get the error message, but I cant really see what it could be :S
Are there any steps Im forgetting here?

The program crashes when I try to move out of the function.

the error message is:
Unhandled exception at 0x619631ea (msvcr90d.dll) in EPGP tool- Console application.exe: 0xC0000005: Access violation reading location 0xcdcdcdc1.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
void   find_new_players( string str , Raider* raider[], string fname){

/*----------------------------- PREPARATION: -------------------------------*/
	MyString tmp(str);								// convert string in to MyString
	string player, buffer, tag_t, end_tag;			// declare strings
	end_tag = tmp.get_last_tag();					// find the last tag in str

	int last_id = raider[last_raider_used]->id();	// Get the last used raider id

/*-----------------------------    STARTS    --------------------------------*/
	while(tag_t != end_tag)
	{
		MyString* player_ptr;							// pointers for substr
		string name, race, guild, clss;
		int sex, level;
 		tag_t = tmp.find_next_tag(tmp.t_start());//find <keyx> from
		buffer = tmp.get_substr(tag_t);		//get player data(between <keyx> and </keyx>
		player_ptr = new MyString(buffer);	//Make a new MyString
		buffer = "";
		buffer = player_ptr->get_substr("name");
		if(buffer == ""){
			cout << "\nERROR! <name> not found in player #" << last_id+1
			     << "!!\nCheck " << fname;
			name = "UNKNOWN";
		}
		else name = buffer;
		buffer = "";
		buffer = player_ptr->get_substr("race");
		if(buffer == ""){
			cout << "\nERROR! <race> not found in player #" << last_id+1
			     << "!!\nCheck " << fname;
			race  = "UNKNOWN";
		}
		else race = buffer;
		buffer = "";
		buffer = player_ptr->get_substr("guild");
		if(buffer == ""){
			cout << "\nERROR! <guild> not found in player #" << last_id+1
			     << "!!\nCheck " << fname;
			guild  = "";
		}
		else guild = buffer;
		buffer = "";
		buffer = player_ptr->get_substr("sex");
		if(buffer == ""){
			cout << "\nERROR! <sex> not found in player #" << last_id+1
			     << "!!\nCheck " << fname;
			sex  = 0;
		}
		else sex = atoi(buffer.c_str());
		buffer = "";
		buffer = player_ptr->get_substr("class");
		if(buffer == ""){
			cout << "\nERROR! <class> not found in player #" << last_id+1
			     << "!!\nCheck " << fname;
			clss  = "UNKNOWN";
		}
		else clss = buffer;
		buffer = "";
		buffer = player_ptr->get_substr("level");
		if(buffer == ""){
			cout << "\nERROR! <level> not found in player #" << last_id+1
			     << "!!\nCheck " << fname;
			level  = 80;
		}
		else level = atoi(buffer.c_str());
		delete player_ptr;

		if(!string_compare( name, raider )){
			last_id++;
			
			raider[++last_raider_used] = new Raider(last_id, name, race, 
												guild, sex, clss, level, 
												0, 1);
		};

		tmp.goto_tag("/level");
		tag_t = tmp.find_next_tag(1 + tmp.t_start()); // </level>
		tag_t = tmp.find_next_tag(1 + tmp.t_start()); // </keyx<>
		tag_t = "<"+tag_t+">";
	};
};
Last edited on
Ok, so it was the destructor that was the problem. But, I cant just NOT have destructors cause they crash! :P

So, could any1 help me identify what the problem with my destructor is?

here is the declaration of the class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class MyString {
private:
	string r_str, tag_start, tag_end,		// main string, <xx> & </xx>
		   filename;
	int    tagp_start,						// <xx> & </xx> position
		   tagp_end,
		   next_start,
		   ts_length,						// length of start, 
		   te_length,						// end &
		   data_length;						// between
	int*   read_ptr;
public:
	MyString( string Log );					// CTOR 1 param
	MyString( string Log, string Filename );// CTOR 2 param
	~MyString();							// DTOR
	void read_file();
...
...
...


and here is the definition of the destructor:

1
2
3
MyString :: 	~MyString(){
	delete read_ptr;
};


any thoughts?
I think you're deleting a pointer that doesn't need to be deleted. delete is associated with pointers to objects created with new.
ahhhh.... doh. Well, now I learned that! :)
Topic archived. No new replies allowed.