Problems deallocating

Started by
13 comments, last by Lacutis 18 years, 9 months ago
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! :)
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.
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.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

BTW, delete[] is used when you called new object[]; otherwise just use delete.
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
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 problemsdelete 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.
moe.ron
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.cppLine: 52Expression: _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!
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...
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
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;}
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" ;)
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!

This topic is closed to new replies.

Advertisement