Jump to content
  • Advertisement

Archived

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

Erzengeldeslichtes

Problems with Delete...

This topic is 5558 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 appologize if this has been asked before, but apparently the search for this forum doesn't like me. (Compiler: Microsoft Visual C++ 6.0) I have a class that has a pointer to a pointer to an object (**object). In this class's create member function, it creates a temp **object, and then news an array that's one larger than the member **object array. It then moves all the pointers over to the temp **object, deletes the member **object and replaces it with the temp one. My problem is that every single delete (even the ones in the destructer) causes an assertion failure (Edit: Usually when I click debug it's somewhere in the middle of the delete statement and it says "User breakpoint called"). What is this and why is it doing it?
        
//stripped down version

class Universe
{
private:
	object **objectList;
	int objectCount;
public:
	Universe();
	~Universe();
	void Create();
};

Universe::Universe()
{
	objectList=NULL;
	objectCount=0;
}

Universe::~Universe()
{
	for(int i=0; i<objectCount;i++)
	{
		delete objectList[i];
	}
	delete[]objectList;
}
void Universe::Create()
{
	object **templist;
	templist = new object*[objectCount];
	if(objectList != null)
	{
		for(int i=0; i<objectCount; i++)
		{
			templist[i] = objectList[i];
		}
		delete[]objectList;
	}
	templist[objectCount] = new object;
	objectList = templist;
	objectCount++;
}

        
Thanks for any assistance. Erzengel des Lichtes Archangel of Light [edited by - Erzengeldeslichtes on April 2, 2003 2:03:57 AM] [edited by - Erzengeldeslichtes on April 2, 2003 2:15:22 AM] [edited by - Erzengeldeslichtes on April 2, 2003 2:29:32 AM]

Share this post


Link to post
Share on other sites
Advertisement
Hi!
What you''re doing wrong is mismatching pointers with pointers to pointers. I suppose you''d expect your Create() to work more or less like this:

object **templist;
templist = new object*[objectCount];

if(objectList != null) {

for(int i=0; i<objectCount; i++) {
templist = objectList[i];
}
delete[]objectList;

}

templist[objectCount] = new object;
objectList = templist;
objectCount++;


Hope this helps.

Share this post


Link to post
Share on other sites
This:

templist = new object*[objectCount];

should be:

templist = new object*[objectCount+1];

..

But with the use of STL, your Create-function could be

void Universe::Create()
{
objectList.push_back(new object);
}

Share this post


Link to post
Share on other sites
quote:
Original post by Erzengeldeslichtes
void Universe::Create()
{
object **templist;
templist = new object[ objectCount ];
if(objectList != null)
{
for(int i=0; i<objectCount; i++)
{
templist[ i ] = objectList[ i ];
}
delete[]objectList;
}
templist[ objectCount ] = new object*;
objectList = templist;
objectCount++;
}



templist = new object[ objectCount ] should not compile because templist is an object ** and new object[objectCount] returns a object *.
new object[ objectCount ] should be new object*[ objectCount+1 ]
new object[ objectCount ]

Share this post


Link to post
Share on other sites
Your Create function is in a bit of a pickle here, because on the one hand you have a variable that keeps track of the number of objects you currently have, while on the other hand you need to know the number of objects in the old array so that the copy can be successful. The first time you call Create, objectCount is 0 and I have no idea how that affects your new allocation.

You can either use two seperate variables, or simply increment objectCount first, use it as the effective new size, and use objectCount-1 as the old size AND newest index. Here's a simple re-write:

      
void Universe::Create()
{
object **templist;
objectCount++; // Represents new object count

templist = new object*[objectCount];
if(objectList != null)
{
// Only go up to objectCount-1, because that's

// all the OLD array had

for(int i=0; i<objectCount-1; i++)
{
templist[i] = objectList[i];
}
delete[]objectList;
}
templist[objectCount-1] = new object;
objectList = templist;
}

Untested, but I hope it works!

EDIT: In light of the other posts, just made logic changes, might still be other errors I overlooked for the greater good

[edited by - Zipster on April 2, 2003 2:27:54 AM]

Share this post


Link to post
Share on other sites
Thanks for all the help. Many posts I caught on my own when running through a test program with JUST my universe code. To the last poster:
That worked perfectly! I suppose I forgot that an object[5] ends up being object[0]-[4]. I remembered that arrays started at 0, but didn't remember that array definitions started at 1. I ended up just modifying my code so that it newed an array of objectNumber+1.

Thanks all!

[edited by - Erzengeldeslichtes on April 2, 2003 2:37:26 AM]

Share this post


Link to post
Share on other sites
And I implement the fix in my larger program and I don''t have any problems with my creation code but when it comes time to delete... I get a "debug asertion failed". I don''t understand it, it''s exactly the same as posted above (with the fix of correct array size) but in the simple program it works and in the complex one it doesn''t. Oig.

After doing alot of step-throughes, I find that it creates the object fine, it gets to delete object[0]; and, even though it shows in my variable list a full fledged object in object[0] it still gives me an asertion failure. Anyone have any idea?

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.

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

Sign me up!