Tacking a -1 onto a string and freeing it... Help Please

Started by
3 comments, last by Destroyer 22 years, 8 months ago
Hi, Alright - I''ve ran into a little bit of a problem - take a look at this code:
  

HRESULT Sprite::Set_Animation_Sprite(char *sequence, POINT pos)
{
	if(animation)
	{
		free ((void*)animation) ;
		animation = (char *)NULL ;
	}

	spritepos = pos; //get the position of the animation;


	animation = (char *)malloc(strlen(sequence) + 1); //allocate enough memory + 1 for animation;

	
	strcpy(animation, sequence); //other way around ?

	
	animation[strlen(sequence)] = -1;

	return DD_OK;
}

  
Some notes - This is part of a DirectX program - but it''s not using DirectX at all except for the HRESULT and the return value - but those can be safely ignored. The main problem here is just the C/C++ programming This function takes a string and a POINT structure and assigns them to members of the Class Sprite. These are animation and spritepos both a char* and a POINT repectivly. The only problem here is with the string manipulation. It should take the string passed to the function - delete the string currently in the char *animation member of the sprite (if there is one) and take the string passed to the function copy it and tack on a -1 to the end of the char* animation. The Problem: When it checks for an animation and frees it - it crops up this error on runtime. >----------------------------------------------------- >Debug Assertion Failed! > >File: dbhheap.c >Line:1011 >Expression: >_CrtsValidHeapPointer(pUserData) > >For more information on how you program assertions >please contact your vendor. >------------------------------------------------------ That above error is in a Dialogue Box that rudely pops up in my program. Why does it generate this error? Thanks, Destroyer
Advertisement
Are you doing anything like this in your code?

  Sprite Function(Sprite A){  // do stuff to sprite A  return A;}  


If so, this is your problem - the destructor is being called on the copy of of the sprite when the function exits, which is freeing the memory pointed to by animation. Attempting to free memory which is already freed will give you an error like this. In order to avoid errors like this, follow these rules....

1. Always set pointers to NULL after you have finished with them. Make sure you check your pointers for NULL (with an ASSERT) before you use them.

2. Always pass classes and structs by reference or by pointer.

3. If you absolutely *have* to pass by value, make sure you create a copy constructor to make a deep copy of the object. (any good book on C++ will explain how to do this)

4. Use new and delete.
Unfortunately that is not the case...

I had made the Class a global and explicity called the constructure with Sprite Whatever = New Sprite();

Does that complicate things further ?
Is ''animation'' ever initialized or set to NULL before this function is called for the first time? Such as in the Sprite constructor?

  if(animation)	{		     free ((void*)animation) ;		     animation = (char *)NULL ;	}  


If animation was never set to NULL or previously initialized, then if(animation) will most likely always be true the first time around except in the freak chance that you happen to have some zeros in that memory at runtime. So then you would be trying to free an allocation that never existed = assert in debug mode maybe a little nastier crash in release mode.


Seeya
Krippy
I think Krippy has it.

But what''s the -1 for?

This topic is closed to new replies.

Advertisement