[Help] Bad Ptr in VC debugger

Posts 16 of 6 · Page 1 of 1
[Help] Bad Ptr in VC debugger
Hello I've been debugging a application i wrote and i've come to a situation.

I've got a some code that im freeing inside a destructor.

Code:
struct Memory
{
public:
	/* constructor and destructor */
	CData( ) 
	{ 
	
	}
	CData( unsigned short datasize, bool readata = true ) 
	{ 
		if( readata ) EncBuffer = (unsigned char*)malloc( datasize );
		else Buffer = (unsigned char*)malloc( datasize );
	}
	~CData( )
	{ 
               // free all my data here....
		delete[] Buffer;
		delete[] EncBuffer;
		Buffer = NULL;
		EncBuffer = NULL;
		
	}
so say now i create another constructor and destructor in another .cpp

Code:
// ofc calling my header to link Memory....
CBaseaddy::CBaseaddy()
{
	
}

CBaseaddy::~CBaseaddy()
{
}

void CBaseaddy::StartEngine()
{
       Memory checking;
       checking->Buffer; // do whatever here
}
any suggestions to why it should show bad ptr in the vc debugger even tho it doesnt crash or give me errors.

Only thing i can think of is that im not freeing the memory inside the local decontructor to show the vc debugger that im freeing it...

any suggestions in how something like this happens or a fix....

Sorry if this sounds confusing....
I don't know what you mean but I believe it's something to do with this:
Code:
checking->Buffer;
What is Buffer? Is it a C++ keyword or an undeclared thingy?
Quote Originally Posted by master131 View Post
I don't know what you mean but I believe it's something to do with this:
Code:
checking->Buffer;
What is Buffer? Is it a C++ keyword or an undeclared thingy?
Its just passing a structure into a ptr then pointing data from it. (really doesnt matter what data it is) The whole point was what HD touched on.

Quote Originally Posted by Hell_Demon View Post
delete[] Buffer;
delete[] EncBuffer;

Only one of those is going to have a value, the other will be a null(or random) pointer, freeing memory from null/random points is not a good idea.

Also, struct Memory should probably class CData o_O
That might explain why every once in a while it crashes due to freeing the null ptr. Thankyou HD ^_^.


Is it better to delete a ptr if your going to switch data from one ptr to another. Or is it better to zero one ptr out and transfer on to another.
Quote Originally Posted by faceofdevil View Post
Its just passing a structure into a ptr then pointing data from it. (really doesnt matter what data it is) The whole point was what HD touched on.



That might explain why every once in a while it crashes due to freeing the null ptr. Thankyou HD ^_^.


Is it better to delete a ptr if your going to switch data from one ptr to another. Or is it better to zero one ptr out and transfer on to another.
Sorry, still new to C++ .
delete[] Buffer;
delete[] EncBuffer;

Only one of those is going to have a value, the other will be a null(or random) pointer, freeing memory from null/random points is not a good idea.

Also, struct Memory should probably class CData o_O
I'd go for adding a bool bEncrypted = false/true depending on which buffer you used in that instance. then only use delete[] EncBuffer if you used encrypted, otherwise delete[] Buffer.

Also make sure that you set the pointer you're not using to NULL(so you can actually check if it's NULL in other places)
Posts 16 of 6 · Page 1 of 1

Post a Reply

Tags for this Thread

None

Need help?