Jump to content
  • Advertisement
Sign in to follow this  
minipie8

assertion error - deleting a struct that contains heap reference

This topic is 5071 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

The program I'm making has a struct that contains arrays from the heap. When I allocated an array of these struct, there is no way to delete that array without an assertion error. I tried making a more general program with the same thing, but that did not work either (that simpler program is listed below). When I debugged the program, I noticed that the destructor was only called once, instead of 10 times, like I expected. The specific error is when the delete operator calls _ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)); Does anyone know of a way to delete the array?
#include <stdio.h>
#include <string.h>

struct thingy
{
protected:
	char * name;
public:
	thingy()
	{
		name = new char[101]; //100 char and a /0
		for (unsigned int i; i < 101; i++)
		{
			name = 0;
		}
	}
	~thingy()
	{
		delete name;
	}
	void setname(char * newname)
	{
		for (unsigned int i; i < 101; i++)
		{
			name = 0;
		}
		for (i = 0; ((i < 100) && (newname != 0)); i++)
		{
			name = newname;
		}
	}
	void getname(char * sendname)
	{
		strcpy(name, sendname);
	}
};

int main()
{
	thingy * a;
	a = new thingy[10];
	delete a; //this causes an assertion error
	return 0;
}

[Edited by - Washu on August 5, 2004 8:44:49 PM]

Share this post


Link to post
Share on other sites
Advertisement
if you do a new[] then you want to call delete[] instead of just delete. it will delete the entire array and not cause le crash.

-me

Share this post


Link to post
Share on other sites
As a general rule, calls to new [] should match delete [], and new should match delete.

ie. delete [] a;

That's not to say that there are not other errors, but that'll fix the present one.

Share this post


Link to post
Share on other sites
also just for fun

this:

for (unsigned int i; i < 101; i++)
{
name = 0;
}





can be replaced with:

memset( name, 0, sizeof(char)*101 );





much faster and just one line of code. :)

-me

Share this post


Link to post
Share on other sites
Quote:
Original post by Palidine
also just for fun

this:
*** Source Snippet Removed ***

can be replaced with:
*** Source Snippet Removed ***

much faster and just one line of code. :)

-me


Or better yet:


name = new char[101]();



To the OP:
You should always use delete[] with new[] and delete with new. Also, you should be including cstdio and cstring, not stdio.h and string.h

Secondly, you shouldn't need to reset the string to 0 when you set it, as the other string should be null terminated. Also, it would be better to return a const pointer to the string for your get method. Unless you know for a fact that you're going to be modifying the string it returns.

And finally: Magic numbers suck. use a named constant that makes sense.

Share this post


Link to post
Share on other sites
Um, just in case no-one picked this up, there's something horribly wrong with:

for (unsigned int i; i < 101; i++)

Like nowhere is i even assigned zero!!! This would probably crap out in release mode. Better to write this:

for (unsigned int i=0; i < 101; i++)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!