Archived

This topic is now archived and is closed to further replies.

delete causes random crash

This topic is 5308 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having trouble with deleting an array. Sometimes it will have a crash the first time, and sometimes it will wait until the third or fourth time the code runs, but it seems that it will eventually crash every time. The error message is an Access Violation at 0x00000005. Here's the initialization code:
	int line = GetNthBotLine(wClass,maxInClass(wClass));	// the line we start writing to

	int size = GetNthBotLine(3,maxInClass(3)) + 17 - line;	// size: total size of file minus our line

	char **stored = 0;
	stored = new char* [size];
	file_man moo(PRESETS_INIT);
	i = line;

	while(!moo.file->eof())		// store all the information in a buffer so we can 

								// write it back later

	{
		strcpy(buf,moo.GetLine(i));
		stored[i - line] = new char [strlen(buf) + 1];
		strcpy(stored[i - line],buf);
		i++;
	}
	while(i < line + size)
	{
		stored[i - line] = 0;
		i++;
	}

 
and here's the delete code:
	for(i = 0; i < size; i++)
	{
		if(stored[i])
		{
			moo.WriteLine(++line,stored[i]);				// write all the old data back

			delete [] stored[i];
		}
	}
	if(stored)
		delete [] stored; // error occurs here

 
The error occurs at the "delete [] stored", but sometimes it takes several function calls before the error occurs. Thanks for any help. edit: changed code tags to source tags [edited by - camomilk on June 5, 2003 11:48:31 AM]

Share this post


Link to post
Share on other sites
Are you sure bslayerw? I have a pointer to an array of char pointers, so I loop thru the array to delete each char array within the bigger array. Then I delete the base array. Are you saying that by deleting the base array, it will delete each of the char arrays within the array too?

Share this post


Link to post
Share on other sites
OK, I think I figured it out, the error was at the first while loop. Apparently sometimes it would go out of bounds on the array. Thanks for trying to help; I guess I didn''t post enough code for people to see the problem.

Share this post


Link to post
Share on other sites
Why don't you use std::string? You can just keep adding characters to it without constant memory handling. I'm actually feeling a little shaky about most of the code you have there. Allocation pointers to a pointer is something that everyone should probably avoid. What is it you are trying to do?


[edited by - MindCode on June 5, 2003 1:30:58 PM]

Share this post


Link to post
Share on other sites
Also, you aren't initializing stored[ i ] to NULL, so your if stored[ i ] check may find something that hasn't actually been allocated. You ARE deleting properly, unlike what that other guy said, as you're deleting a character array. Just make sure you initialize all your pointers to NULL, otherwise you will have issues in the future, and make sure you don't over-run the bounds .

--- Edit ---
Kept italicizing my [ i ], so I had to add the spaces .

Oops, just noticed you intialize the remaining ones to NULL afterwards, but make sure your first loop checks if i < size + line, otherwise you'll have the bounding issues.

[edited by - Ready4Dis on June 5, 2003 7:58:57 PM]

Share this post


Link to post
Share on other sites