//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++;
}
Problems with Delete...
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?
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]
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;<br>}<br>delete[]objectList;<br><br>}<br><br>templist[objectCount] = new object;<br>objectList = templist;<br>objectCount++;<br><br><br>Hope this helps. </i>
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;<br>}<br>delete[]objectList;<br><br>}<br><br>templist[objectCount] = new object;<br>objectList = templist;<br>objectCount++;<br><br><br>Hope this helps. </i>
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);
}
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);
}
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 = objectList;<br> }<br> delete[]objectList;<br> }<br> templist[ objectCount ] = new object*;<br> objectList = templist;<br> objectCount++;<br>}<br> <hr height=1 noshade></SPAN></BLOCKQUOTE> <br><br><tt>templist = new object[ objectCount ]</tt> should not compile because templist is an <tt>object **</tt> and <tt>new object[objectCount]</tt> returns a <tt>object *</tt>.<br><tt>new object[ objectCount ]</tt> should be <tt>new object*[ objectCount<b>+1</b> ]</tt><br><tt>new object[ objectCount ]</tt>
Your
You can either use two seperate variables, or simply increment
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]
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]
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]
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]
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?
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?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement