Jump to content
  • Advertisement

Archived

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

skow

Problem with object with allocated array (c++)

This topic is 5331 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 been triing to figure this one out for quite a while: I have an object that has a private variable that is an object that has a pointer that during construction is allocated as the following: explode = new part2 [60]; I have a destructor that checks to make sure the pointer isn’t == to null as shown, then deletes the array. if (explode != NULL) delete [] explode; The problem is with this in my destructor my program gives me an assertion error “_CrtIsValidHeapPointer(pUserData)” as soon as I start my GL looping. With it removed everything works fine (except for a memory leak I assume). I try debugging with a breakpoint at the start of the destructor, but the breakpoint is never hit. So from what I understand is the destructor is never called but some code in it is causing me to get the assertion error. Wow this one has me cofused.

Share this post


Link to post
Share on other sites
Advertisement
If code in the destructor is causing an error, then it''s getting run.

My first suspicion is that you''re deleteing it more than once. Try this:

if( explode != NULL )
{
delete [] explode;
explode = NULL;
}

And, in your constructor, set the pointer to NULL to start with. That way the pointer is always either pointing to valud data or NULL, no questions asked.

I like pie.

Share this post


Link to post
Share on other sites
My guess is that you haven''t written your own copy constructor or assign operator, and you make copies of the said object. So delete[] explode; will be called twice on same memory.

so if your class is like this:

class X {
public:
X() {
explode = new part2[60];
}

~X() {
if (explode != NULL)
delete [] explode;
}
private:
...
};


try adding private copy constructor and assignment operators and see if it still compiles:


class X {
public:
X() {
explode = new part2[60];
}

~X() {
if (explode != NULL)
delete [] explode;
}
private:
X(const X& x);
X& operator=(const X& x);
...
};

If it doesn''t, it tells you''re using either of those two somewhere, and that part causes the pointer "explode" to be duplicated in two objects yet the memory isn''t duplicated.

Share this post


Link to post
Share on other sites
quote:
Original post by RenderTarget
if( explode != NULL )
{
delete [] explode;
explode = NULL;
}
This won''t help anything, because after the destructor is done (in following line probably), the variable "explode" ceases to exist, and assignment to it''s value was useless.

Share this post


Link to post
Share on other sites
quote:
Original post by civguy
quote:
Original post by RenderTarget
if( explode != NULL )
{
delete [] explode;
explode = NULL;
}
This won''t help anything, because after the destructor is done (in following line probably), the variable "explode" ceases to exist, and assignment to it''s value was useless.




That''s not true. Deleting a pointer frees the memory pointed to by the number in the pointer. It doesn''t change the value of the pointer itself. Setting explode to NULL is a good thing.

Share this post


Link to post
Share on other sites
Wow I love this place, fast good responces. I''ll try some of the above and report back.

Thanks.

Share this post


Link to post
Share on other sites
quote:
Original post by Rick Scott
That's not true. Deleting a pointer frees the memory pointed to by the number in the pointer. It doesn't change the value of the pointer itself. Setting explode to NULL is a good thing.
What I meant that setting explode to NULL affects nothing. I know it sets explode's value to 0, but that's meaningless. It's like this piece of code:

{
int* x = new int[10];
...
delete[] x;
x = 0;
}

What does x=0 help here? The variable x ceases to exist right after the assignment, so the assignment x=0 was meaningless. In original poster's example, we were in the destructor of the class, so the variable 'explode' will vanish soon, from his description I'd the destructor contains no more code and 'explode' will vanish right away.

[edited by - civguy on January 11, 2004 5:20:29 PM]

Share this post


Link to post
Share on other sites
if( explode != NULL )
{
delete [] explode;
explode = NULL;
}

Did nothing for me, but creating a copy constructor fixed the assertion error. I have a new problem where the array I make is now empty, but that is another bug ill find.

Thanks for the help.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!