Public Group

# Free or realloc or crashes malloc

This topic is 4905 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi everyone and guys including girls, I am so crazy and in despair about memory management in WIN32.I can't bare it anymore I thought of asking experts here.Bare with me with this long post because I really at first didn't know how to explain the situation I am facing and writing a long post is really hard to let you guys help me but I wrote it anyway. I have a problem with memory management in WIN32.As in the following source code post is all about string manipulations.I somehow malloc on the same variable twice in a row because I needed it.And also copied is a global variable I need to used to pass it back to some other array. The full code is listed below.I really want to know how could I malloc a better way than this.I actually didn't get error at VS.NET DEBUG mode,of course it helps patches up and there in DEBUG mode.But I compile in RELEASE mode (inside IDE it runs smooth) but when run outside of the IDE it crashes.I know is some malloc stuff I did.Hope someone could help me out p/s:I believe there are also buffer overrun,overflow or maybe not.Now my wheel of fortune game is stuck!:(
//char *copied is a global variable
//char *test is a global variable

int SqlChat::ExtractData()
{
char str[256];
int flag = 0;
char *test;
int i;
int value=0;
TTCounter=0;

FILE *myfile=fopen("sql.txt","r");

while(!feof(myfile))
{
if(flag == 0)
{
fgets(str,256,myfile);
test=(char*)malloc(strlen(str)-1);

for(i=0;i<strlen(str)-1;i++)
test = str;
test='\0';

TempText2[TTCounter]=(LPCTSTR)test;
TTCounter++;
flag = 1;

}
else if(flag==1)
{
fgets(str,256,myfile);
test=(char*)malloc(strlen(str)-1);

for(i=0;i<strlen(str)-1;i++)
test = str;
test='\0';

//cChat.setText((LPCTSTR)test);
value=setCText(test);
flag=2;
}
else if(flag==2)
break;

}
fclose(myfile);

return value;
}

int SqlChat::setCText(char *string)
{
int i,j;
char *temp1;
int StringCnt=0;
int tempC=0;
int textLineNO = 1;
int squareBox;
int mySpc=1;
int iTemp=0;
int StringCntTemp=0;

temp=string;

temp1=(char*)malloc(29);//20); //maximum size can be allocated

for(i=0;i<strlen(temp);i++)
{

if(temp == ' ')
{
iTemp = i;
StringCntTemp=StringCnt;
while(temp[iTemp+mySpc] != ' ' )
{
mySpc++;
if(temp[iTemp+mySpc] == '\0')
break;
}

if((StringCntTemp+(mySpc-1))>=28)//18)  //2 spaces less coz for '\0' and buffer overflow
{
StringCnt=29; //21 maximize it extra 1 coz top trigger the statement
mySpc=1;
}
else
mySpc=1;

}
if(StringCnt<28)  //1 space less than in case of '\0'
{
temp1[tempC]=temp;
tempC++;
}

if(temp == '\t')
{
if(squareBox == 0)
StringCnt+=1;
else if(squareBox == 1)
StringCnt+=2;
else if(squareBox == 2)
{
StringCnt+=2;
squareBox=0;
}
squareBox++;
}
else
StringCnt++;

if(StringCnt>=29) //must be more than the actual one

{
temp1[tempC]='\0';

CopyLPCTSTR(temp1, textLineNO);
TempText2[TTCounter]=(LPCTSTR)copied;
textLineNO++;
TTCounter++;

for(j=0;j<strlen(temp1);j++)
temp1[j]=(const char)"";

tempC=0;
StringCnt=0;

}

}
temp1[tempC]='\0';
CopyLPCTSTR(temp1, textLineNO);
textLineNO++;
TempText2[TTCounter]=(LPCTSTR)copied;

//	tempText2[counter]=(LPCTSTR)temp1;

return TTCounter;
//return the counter for how many lines
}
void SqlChat::CopyLPCTSTR(char word[], int ntextLineNO)
{
copied=(char *)malloc(strlen(word)); //buffer underflow + 1

int i;
int t = strlen(word); //buffer overflow +1
int NoOfMW = 0;
int NoOfil = 0;
int NoOfChar = 0;
int emotNum = 0;

for(i=0;i<t;i++)
{
if(word == 'm' || word == 'W')
{
NoOfChar = NoOfChar +6;

}
else if(word == 'w')
{
NoOfChar = NoOfChar +5;

}
else if(word == 'M'||word == '#')
{

NoOfChar = NoOfChar +4;

}
else if (word == 'I'||word == 'J'||word == 'c')
{
//Do nothing

}
else if (word == 'A'||word == 'D'||word == 'G' ||word == 'H'||word == 'N'||word == 'O'||word == 'Q'||
word == 'R'||word == 'U')
{
NoOfChar = NoOfChar +3;

}
else if (word == 'B'||word == 'C'||word == 'E'
||word == 'K'||
word == 'P'||
word == 'S'||word == 'V'||
word == 'X'||word == 'Y'||word == 'Z'||word == 'b'||word == 'd'||word == 'g'||word=='n'||
word == 'o'||word == 'p'||word == 'q'||word == 'u'||word == '1'||word == '2'||
word == '3'||word == '4'||word == '5'||word == '6'||word == '7'||word == '8'||
word == '9'||word == '0'||word =='*')
{
NoOfChar = NoOfChar + 2;

}
else if (word=='f'||word == 't'||word == 'r')
{

NoOfChar = NoOfChar -1;
}

else if (word == 'i' || word == 'j'|| word == 'l'||word == ' '||word == '.'||word == ',')
{

NoOfChar = NoOfChar - 2;
}

else if(word == ':')
{
word = ' \t';

}

else if (word == ')')
{

word = '\t';

if (i == 1)
{
EmoticonXPos[ntextLineNO][emotNum]=0;
}
else
{
EmoticonXPos[ntextLineNO][emotNum]=(i-2)*6+emotNum*15+NoOfChar;
}

emotNum++;

}
else
{
NoOfChar++;
}

copied=word;

}

copied='\0';
EmotNO [ntextLineNO] = emotNum;
}


Thanx to others who help me go through the previous problems while creating my game.Thanx a bunch and hope someone could really guide me. in despair, Commy2005

##### Share on other sites
    test = malloc(strlen(str)-1);    ...    for (i = 0; i < strlen(str)-1; i++ )    {        ...    }    test = '\0';
test has room for strlen(str)-1 bytes. When the for-loop is done, i has the value strlen(str), so setting test is writing a 0 past the end of test and is trashing data used to manage memory allocation.

First of all in case you didn't know it, strlen() returns the number of characters in the string not including the 0 terminator. Now here is what you code should probably look like:
    test = malloc(strlen(str)+1); // +1 for the terminator    ...    for (i = 0; i < strlen(str); i++ )    {        ...    }    test = '\0';
See the difference?

##### Share on other sites
Thanx for your reply JohnBolton,I actually wondering is there another way to malloc the same variable in the way if the str was longer than before?

Coz I actually change to the way like you recommended,it crashes when the test was longer than the previous malloc string.when I malloc the new str which is longer than the previous ones ,it crashes.(Release can't even run but outside of IDE could run RELEASE but if the str is longer than the previous ones it crashes)

Thanx for your recommendation.seems like is a tragic of memory allocation then.any help anymore??

in despair,
Commy2005 :)

##### Share on other sites
Ok, I've tried HARD (like, for an hour or so) to figure out how your code is supposed to work out, but I honestly can't say I understand exactly what processing you're doing.

Are you simply trying to break down the input text into lines of 28 characters?

##### Share on other sites
Incidentally

const int NumOfChar_table[96] ={//      !   "   #   \$   %   &   '   (   )   *   +   ,   -   .   /      -2,  1,  1,  4,  1,  1,  1,  1,  1, 99,  2,  1, -2,  1, -2,  1,  //  0   1   2   3   4   5   6   7   8   9   :   ;   <   =   >   ?    2,  2,  2,  2,  2,  2,  2,  2,  2,  2, 99,  1,  1,  1,  1,  1, //  @   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O    1,  3,  2,  2,  3,  2,  1,  3,  3,  0,  0,  2,  1,  4,  3,  3,//  P   Q   R   S   T   U   V   W   X   Y   Z   [   \   ]   ^   _    2,  3,  3,  2,  1,  3,  2,  6,  2,  2,  2,  1,  1,  1,  1,  1,//     a   b   c   d   e   f   g   h   i   j   k   l   m   n   o    1,  1,  1,  0,  2,  1, -1,  2,  1, -2, -2,  1, -2,  6,  2,  2,//  p   q   r   s   t   u   v   w   x   y   z   {   |   }   ~  DEL    2,  2, -1,  1, -1,  2,  1,  5,  1,  1,  1,  1,  1,  1,  1,  1,}

if not ':' or ')', NumOfChar += NumOfChar_table[word-' '];

Might be handy...

##### Share on other sites
Yeah Fruny thanx for your reply..really appreciated.Yeah..i was trying to do string manipulations.Somehow there are some weird stuff happened in the code.
the string manipulations part get out of hand.it started to crash like crazy.

I don't get your recommendations but I will try to look at it again.
I actually debug and debug and came to an end of the solution.

before the code paste above there is another function which suppose to open a file again.But this time I realize something very weird happened....take a look guys..

int SqlChat::UseSql(){  char		szSQL[ 200 ], aszFlds[ 25 ][ 25 ], szDB[ 50 ] ;  const  char   *pszT;  int			i, j, k, l, x ;  MYSQL		*myData ;  MYSQL_RES	*res ;  MYSQL_FIELD	*fd ;  MYSQL_ROW	row ;  char qbuf[160];  int  flag=1;    //if (nextData == 1) // {    FILE *myfile=fopen("sql.txt","w");   //<-the problem was ExtractData(...)above  if ( (myData = mysql_init((MYSQL*) 0)) &&        mysql_real_connect( myData, "xxx.xxx.xxx.230", "xxxx", "xxxx", "xxxx", MYSQL_PORT,NULL, 0 ) )............ //here are the codes which I didn't post coz is not relevant yet....	  fprintf(myfile,"%s\n",(((row[k]==NULL)||(!strlen(row[k])))?"NULL":row[k])) ; fclose(myfile);mysql_close( myData ) ;}`

I actually change the SqlChat::ExtractData(...)

.....
.....
FILE *myfile=fopen("sql.txt","rw"); <- when only "r", didn't work,"rw" NO MORE crashes ??????
......
.....

Anyone can actually tell me why adding a "w" to it will not crash but taking off the "w" will crash???

But for the time being I am crahing inside IDE but on the release folder it doesn't crash...But at least I solve the problem a little bit...:)

Thanx for others who help me out and giving me recommendations ,and looking through my codes for hours :) and was hard to understand.:)

Thanx guys and Fruny you are right I am doing string manipulations.

lighten up,
Commy2005

##### Share on other sites
'w' will create the file if it does not exist.

What I'd like to know is what string manipulations you (think you) are doing.

It's much easier with std::string and std::vector...

##### Share on other sites
It was a chatroom for my game (wheel of fortune) I actually trying to count pixels on the chatroom width.So I could actually fit the string there with animation nickname and text multiline.I tried using Tabtextout and others didn't really work out nicely.

didn't get to use RichEditOLE(...) stuff class to work coz cannot put in emotions,so I created my own way of writing a text out on the chatroom with gif animation (as emoticons)Example every where(on the net) didn't get it to work.Not using MFC at all.(coz me using WIN32)

Tried using DrawText(...) with DT_WORDBREAK didn't work out nicely.Coz my chatroom needed to use emoticons.So determine the emoticons position each line of the text is a class with x,y position.

When putting the emoticons,seems like the "m","w","M","W","i","I" have different pixel width.Those NoOFchars is to determine plus/minus of pixel width.

So Came out of a way to put emoticons on the chatroom with text wrapping around it.With Animations as well scrolling up as well.So things go crazy and wacky.When I found out the r and w thing.Data is grab from SQL server and stuff....

Now the sql.txt file do exist after creation on the first function.But how can it not work when is already working.:)

lighten up,
COmmy2005

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5
JoeJ
19

• 11
• 15
• 9
• 10
• 13
• ### Forum Statistics

• Total Topics
633004
• Total Posts
3009837
• ### Who's Online (See full list)

There are no registered users currently online

×