Jump to content
  • Advertisement
Sign in to follow this  
storage

Problems deallocating

This topic is 4868 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'm getting this error message (MS VC++ 2005 Beta 2) when I'm running my application:
Unhandled exception at 0x1024be5b (msvcr80d.dll) in TestApp.exe: 0xC0000005: Access violation reading location 0xcdcdcdc1.
It occurs here:
ctest.h:
...
		unsigned char *data;
...

ctest.cpp:
int CTest::Free()
{
	if (!this->data)
		return -1;

	delete this->data; // this line

	return 0;
}


I've also tried "delete[] this->data;" instead of "delete this->data;". How do I fix this? Thanks in advance! :)

Share this post


Link to post
Share on other sites
Advertisement
It sounds like you are calling Free() on a bad pointer to object. Check to make sure that your object is valid when making the call.

Share this post


Link to post
Share on other sites
0xcdcdcdcd is a special value MSVC uses for uninitialized variables. You didn't use data before nor set it to any useful value.

In the constructor of your CTest class, set data to NULL in the initializer list.



CTest::CTest() :
data( NULL )
{
}


Variables have not, are not and will not be set to any value if you don't do so in a constructor.

Share this post


Link to post
Share on other sites
Also deleting a NULL ptr has no adverse affect. Take the following for example


foo *pFoo = 0;

delete pFoo; // no problems...

pFoo = new foo;

delete pFoo; // no problems

delete pFoo; // **BOOM**


Generally when delete causes a problem, its because you are attempting to delete memory that has already been deleted or otherwise the pointer is pointing to chode memory.

Share this post


Link to post
Share on other sites
Thanks everyone! :)

I tried taking the class apart, and rebuild it from scratch, using the advice I was given. The class is a wrapper around Corona that loads the image to pixel data.

Constructors:

CTest::CTest() : data(0)
{
}

CTest::CTest(std::string filename) : data(0)
{
this->Load(filename);
}



Destructor:

CTest::~CTest()
{
this->Free();
}



New Free() method:

void CTest::Free()
{
if (!this->valid)
return;

if (!this->data)
return;

delete this->data; // this line
}



Here is the code in main.cpp that uses the class that crashes:

CTest *Image = new CTest;

Image->Load("box.png");

int w = Image->width;
int h = Image->height;
unsigned char *data= Image->getdata();

delete Image; // this line



I'm now given another error (in a message box, not the normal one):

Debug Assertion Failed!

Program: ...
File: dbgdel.cpp
Line: 52

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)



Here is the call stack:

msvcr80d.dll!operator delete(void * pUserData=0x02184960) Line 52 + 0x51 bytes C++
> TestApp.exe!CTest::Free() Line 51 + 0x18 bytes C++
TestApp.exe!main() Line 61 C++
TestApp.exe!mainCRTStartup() Line 558 + 0x19 bytes C
kernel32.dll!RegisterWaitForInputIdle() + 0x49 bytes


Thanks in advance!

Share this post


Link to post
Share on other sites
At a guess, it would seem to mean that you're trying to delete memory that was not allocated by your program. That's all I can tell without seeing more of what's going on...

Share this post


Link to post
Share on other sites
Quote:
Original post by TDragon
At a guess, it would seem to mean that you're trying to delete memory that was not allocated by your program. That's all I can tell without seeing more of what's going on...


Thanks! But I can't see how the memory is NOT allocated.

CTest::Load:

int CTest::Load(std::string filename)
{
corona::Image *img = corona::OpenImage(filename.c_str());

if (!img)
{
printf("failed to load image file\n");
return -1;
}

this->width = img->getWidth();
this->height = img->getHeight();
//this->data = new unsigned char[(this->width * this->height) * 4];
this->data = (unsigned char *)img->getPixels();

return 0;
}

Share this post


Link to post
Share on other sites
Perhaps your OpenImage function isn't returning a NULL if it fails to load the image? Post the contents of "OpenImage()" and maybe that will show us where the problem is.

My guess is that openimage returns a pointer to a temporary created inside the function, which is a valid address at that point, and later on you are trying to deallocate a pointer which points to memory that isn't valid anymore.

Also as a side note, I had a little chuckle with the thread title off the main page "trouble deallocating, by storage" ;)

Share this post


Link to post
Share on other sites
Quote:
Original post by Lacutis
Perhaps your OpenImage function isn't returning a NULL if it fails to load the image? Post the contents of "OpenImage()" and maybe that will show us where the problem is.

My guess is that openimage returns a pointer to a temporary created inside the function, which is a valid address at that point, and later on you are trying to deallocate a pointer which points to memory that isn't valid anymore.

Also as a side note, I had a little chuckle with the thread title off the main page "trouble deallocating, by storage" ;)


Haha :)

OpenImage isn't written by me, it's a part of the Corona image loader (link), so I have no idea what's going on in there, unfortunately :(.

How can I copy the data OpenImage is pointing to, and then point to it, after it has been deleted? memcpy()? And if so, how do I use it? :)

Thanks!

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.

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!