Jump to content
  • Advertisement

Archived

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

Orpheum

delete [] operator giving debug errors...

This topic is 6242 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''ve run out of ideas here, I''ve searched through all of my texts and the MSDN library to no avail. My problem is this: Im trying to use delete [] to delete some dynamically allocated arrays of chars (should be easy right??), but when the I run the program, I get the following error. Debug Error! Program: c:\blah\blah.exe DAMAGE after Normal block (#42) at 0x00300050 GRR! I hate that stupid message... The program works perfectly (aside from leaking massive amounts of memmory if I take out the delete [] statements. Anyway, the the arrays are allocated like this: void GDIMapGen::setMapName(char* szName, int iSize) { szMapName = new char[iSize]; strcpy(szMapName, szName); iMapNameSize = strlen(szMapName); } and void GDIMapGen::setTileSetName(char* szTile, int iSize) { szTSetName = new char[iSize]; strcpy(szTSetName, szTile); iTSetNameSize = strlen(szTSetName); } and then deleted in the destructor (where it bitches at me) GDIMapGen::~GDIMapGen() { delete [] szMapName; //SHOULD NOT BREAK!!!!!!! delete [] szTSetName; // } Ok, there it is... any help is most appreciated, and I can provide source files if needed... just email me at orpheum@us.ibm.com. Thanks!!

Share this post


Link to post
Share on other sites
Advertisement
The new and delete operators can only be used with pointers, so if szMapName and/or szTSetName aren''t pointers, delete[] would fail. I''ll repaste the code and make any possible changes in bold:


char *szMapName; // Declares pointer
char *szTSetName; // Delclares pointer

void GDIMapGen::setMapName(char* szName, int iSize)
{
szMapName = new char[iSize];
strcpy(szMapName, szName);

iMapNameSize = strlen(szMapName);
}

and

void GDIMapGen::setTileSetName(char* szTile, int iSize)
{
szTSetName = new char[iSize];

strcpy(szTSetName, szTile);

iTSetNameSize = strlen(szTSetName);
}

and then deleted in the destructor (where it bitches at me)

GDIMapGen::~GDIMapGen()
{
delete [] szMapName; //SHOULD NOT BREAK!!!!!!!
delete [] szTSetName; //
}


This should work unless this is what you''re already doing!

PCMCIA - People Can't Memorize Computer Industry Acronyms
ISDN - It Still Does Nothing
APPLE - Arrogance Produces Profit-Losing Entity
SCSI - System Can't See It
DOS - Defunct Operating System
BASIC - Bill's Attempt to Seize Industry Control
IBM - I Blame Microsoft
DEC - Do Expect Cuts
CD-ROM - Consumer Device, Rendered Obsolete in Months
OS/2 - Obsolete Soon, Too.
WWW - World Wide Wait
MACINTOSH - Most Applications Crash; If Not, The Operating System Hangs

Share this post


Link to post
Share on other sites
I''m not sure exactly what that error message means, but it sounds like the linked list that malloc (and new) uses to keep track of the memory blocks is getting corrupted.

Try using:
szMapName = new char[strlen(szName)+1];
strcpy(szMapName, szName);

The thing I''m getting at, is that iSize might not be large enough, and you might be running beyond the end of the dynamic memory you are creating. Unless that memory was being used, the error could go unnoticed easily.

Jesse Chounard

Share this post


Link to post
Share on other sites
In my opinion the use of the array delte operator (delete []) isn''t correct. As far as I know this version calls the destructor for evry object in the array. However a delete for a singe char woudn''t make sence. What you want is to delete a single object. So try to use delete without the square brackets.

Share this post


Link to post
Share on other sites
I haven''t read through Zipsters code, but I think Jesse has a point
Volker, you should call delete[].
char *ch = new char; //This is where you use delete
You''re working with a pointer, and c++ doesn''t differentiate between a pointer to an individual element and a pointer to the initial element of an array, which is why you have to tell it.

Share this post


Link to post
Share on other sites
Thanks guys! I needed to use iSize + 1 for my char arrays, so I was overwriting memmory. You guys are much cooler thatn the chumps at programmersheaven.com.... they still havent responded to my post!

Share this post


Link to post
Share on other sites
quote:
Original post by VolkerG
In my opinion the use of the array delte operator (delete []) isn''t correct. As far as I know this version calls the destructor for evry object in the array. However a delete for a singe char woudn''t make sence. What you want is to delete a single object. So try to use delete without the square brackets.


Nope. The new and delete operators both come in two forms. When you do this:

char *pChar = new char[10];

You''re actually using the new[] operator. This is when you''d want to use it''s counterpart, delete[]. As in:

delete[] pChar;

So, when you use new, you use delete. When you use new[], you need delete[].

Jesse Chounard

Share this post


Link to post
Share on other sites
Just to add to the information:
"Damage after normal block" is an error you get when overrunning a dynamic array into the area that''s used to denote the end of the array. If you get this error, you know you''ve made your buffer too small or your write too large.

#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**

Share this post


Link to post
Share on other sites
Since I like to hear myself talk today. (I don''t want to work, I just want to bang on the drums all day...)

As other people have said, you should use ''delete []'' on memory allocated with ''new []''.

On many platforms, using just plain old ''delete'' seems to work, but it really doesn''t. My brother had a case where the program worked in debug just fine, but when he did the release build, CRASH!!!!

Tim

Share this post


Link to post
Share on other sites
... Just to put in a little C++ bigotry ... this is why
std::string's where invented.



#include <string>

class GDIMapGen {
...
std::string mapName;
};

void GDIMapGen::setMapName(const string &str)
{
mapName = str;
}



When you need to get at the characters inside the std::string object (i.e.: for opening files, etc), just use mapName.c_str(), which returns a (const char *).

-- Pryankster


Nice and efficient.

Edited by - Pryankster on 5/4/00 12:26:52 PM

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!