Annoying Litle Bug....Need Some Help

Started by
3 comments, last by Electron 19 years, 9 months ago
Hey, Alrite I've banged my head off the wall for the last two days and still havent't come up with a solution to my problem. In essense what I want to do is receive a packet off the socket, read through and parse it then reset the message using this method:

void CMessageManager::messageResetMessage( CMessage* pMsg )
{
	// clear the data buffer
	memset( pMsg->m_szData, 0, sizeof( pMsg->m_szData ) );

	// reset the message size and
	// overflowed flag
	pMsg->m_iCurrentSize = 0;
	pMsg->m_bHasOverflowed = false;
}
m_szData is an array of 4096 unsigned chars ex. unsigned char* m_szData = new unsigned char[ 4096 ]; My problem comes in that when I call this method, it doesn't seem to be clearing the buffer because in following messages I get remnants from the previous if it was longer than the new one. Does anyone see anything wrong with this method, or is this perhaps not the cause of my problem? Thanks, Permafried-
Advertisement
sizeof( pMsg->m_szData ); is returning the size of a pointer, not the size of 4096 unsigned chars. replace sizeof( pMsg->m_szData ) with sizeof( unsigned char ) * 4096 and you should be fine.
Thanks Hamster, that was part of my problem, my other was the method that I used to read strings out of packets contained a static char buffer, and I wasn't clearing it on every pass so naturally I was ending up with loads of garbage leftover @_@.

Thanks again,

Permafried-
sizeof(unsigned char) is always 1, by definition.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
If it is a string, the the problem is (probably) not the receiever, but the sender who forgets to end the string with a NULL character. If you copy the string with strlen or something, notice that the string actually must be strlen()+1 in size, in order to include the NULL character. Or just put the NULL character in manually.

void setMsg(CMessage* pMsg, unsinged char str){   pMsg->m_szData = new unsigned char[4096];   for (int i = 0; i < strlen(str); i++)     pMsg->m_szData = str;      pMsg->m_szData[strlen(str)] = 0; //Add the NULL character};
--Electron"The truth can be changed simply by the way you accept it.""'General failure trying to read from file' - who is General Failure, and why is he reading my file??"

This topic is closed to new replies.

Advertisement