问题描述
- C语言mysql的内存溢出问题
- int String_GetLength(char* s)
{
int i=0;
while (*s)
{
i++;
s++;
}
return i;
}char* String_Cat(char* s1 int s1_size char* s2)
{
int size1 = String_GetLength(s1);
int size2 = String_GetLength(s2);
if((size2 + size1) < (s1_size - 1))
{
int i = 0;
for(i = 0; i < size2; i++)
{
s1[size1 + i] = s2[i];
}
}
return s1;
}char* sasql_selectField_byWhereAndLimit( char _tablename char *_field char *_where char *_limit )
{
//MYSQL_RES *_mysql_result;
//MYSQL_ROW _mysql_row;
char sqlstr[1024];
//char result = (char*)malloc(sizeof(char) * 81920);
char result[81920] = {''};
//result = {''};
//char _retstr[81921];
MYSQL_FIELD *my_field;
sprintf(sqlstrselect %s from%s
%s %s "" _field _tablename _where _limit);
if (mysql_set_character_set(&mysqlgbk"")) {
fprintf (stderr 错误 %sn"" mysql_error(&mysql));
}
if(!mysql_query(&mysql sqlstr))
{
int num_row = 0;
mysql_result = mysql_store_result(&mysql);
num_row = mysql_num_rows(mysql_result);
if(num_row > 0)
{
int j = 1;
for(j = 1; j <= num_row; j++)
{
mysql_row = mysql_fetch_row(mysql_result);
my_field = mysql_fetch_fields(mysql_result);
int _len = mysql_num_fields(mysql_result);
int i = 0;
for(i = 0; i < _len; i++)
{
String_Cat(result 81920 my_field[i].name);
String_Cat(result 81920:"");
String_Cat(result 81920 mysql_row[i]);//String_Cat (result my_field[i].name);
//strcat (result:"");
//strcat (result mysql_row[i]);
if (i != _len - 1)
{
String_Cat(result 81920);
//strcat (result);
}
}if (j != num_row)
{
String_Cat(result 81920#"");
//strcat (result#"");
}
}
//sprintf(result%s%s%s%s%s%s%s""mysql_row[0]mysql_row[1]mysql_row[2]mysql_row[3]mysql_row[4]mysql_row[5]mysql_row[6]);
mysql_free_result(mysql_result);//strcpysafe(_retstr sizeof(_retstr) result);
//free(result);return result;
}
else
{
mysql_free_result(mysql_result);
return ""err"";
}
}
else
{
return ""err"";
}
}这段代码运行过程中,有一定的几率报:
*** glibc detected *** ./gmsvjt: realloc(): invalid pointer: 0x0bf03d18 ***
======= Backtrace: =========
/lib/libc.so.6(realloc+0x35d)[0x6b226d]
./gmsvjt[0x827b2cb]
./gmsvjt[0x8276336]
./gmsvjt[0x82790da]
./gmsvjt[0x827ab7f]
./gmsvjt[0x82865e7]
./gmsvjt[0x82774b3]
./gmsvjt[0x82743aa]
./gmsvjt[0x8276100]
./gmsvjt[0x8277aa8]
./gmsvjt[0x82743f4]
./gmsvjt[0x82636df]
./gmsvjt[0x806ff3c]
./gmsvjt[0x808e620]
./gmsvjt[0x805b45e]
./gmsvjt[0x804a986]
./gmsvjt[0x804aeaf]
/lib/libc.so.6(__libc_start_main+0xdc)[0x659ebc]
./gmsvjt[0x804a361]
======= Memory map: ========
00101000-00228000 r-xp 00000000 03:01 3441494 /usr/lib/mysql/libmysqlclient.so.16.0.0
00228000-0026f000 rwxp 00127000 03:01 3441494 /usr/lib/mysql/libmysqlclient.so.16.0.0
0026f000-002b5000 r-xp 00000000 03:01 786650 /lib/libssl.so.0.9.8e
002b5000-002b9000 rwxp 00045000 03:01 786650 /lib/libssl.so.0.9.8e
002b9000-002c3000 r-xp 00000000 03:01 786471 /lib/libnss_files-2.5.so
002c3000-002c4000 r-xp 00009000 03:01 786471 /lib/libnss_files-2.5.so
002c4000-002c5000 rwxp 0000a000 03:01 786471 /lib/libnss_files-2.5.so
00621000-0063c000 r-xp 00000000 03:01 786640 /lib/ld-2.5.so
0063c000-0063d000 r-xp 0001a000 03:01 786640 /lib/ld-2.5.so
0063d000-0063e000 rwxp 0001b000 03:01 786640 /lib/ld-2.5.so
00644000-0079b000 r-xp 00000000 03:01 786641 /lib/libc-2.5.so
0079b000-0079d000 r-xp 00157000 03:01 786641 /lib/libc-2.5.so
0079d000-0079e000 rwxp 00159000 03:01 786641 /lib/libc-2.5.so
0079e000-007a1000 rwxp 0079e000 00:00 0
007a3000-007a6000 r-xp 00000000 03:01 786642 /lib/libdl-2.5.so
007a6000-007a7000 r-xp 00002000 03:01 786642 /lib/libdl-2.5.so
007a7000-007a8000 rwxp 00003000 03:01 786642 /lib/libdl-2.5.so
007aa000-007d1000 r-xp 00000000 03:01 786645 /lib/libm-2.5.so
007d1000-007d2000 r-xp 00026000 03:01 786645 /lib/libm-2.5.so
007d2000-007d3000 rwxp 00027000 03:01 786645 /lib/libm-2.5.so
007d5000-007eb000 r-xp 00000000 03:01 786643 /lib/libpthread-2.5.so
007eb000-007ec000 r-xp 00015000 03:01 786643 /lib/libpthread-2.5.so
007ec000-007ed000 rwxp 00016000 03:01 786643 /lib/libpthread-2.5.so
007ed000-007ef000 rwxp 007ed000 00:00 0
007f1000-00803000 r-xp 00000000 03:01 786646 /lib/libz.so.1.2.3
00803000-00804000 rwxp 00011000 03:01 786646 /lib/libz.so.1.2.3
00811000-00827000 r-xp 00000000 03:01 786654 /lib/libselinux.so.1
00827000-00829000 rwxp 00015000 03:01 786654 /lib/libselinux.so.1
0082b000-00866000 r-xp 00000000 03:01 786652 /lib/libsepol.so.1
00866000-00867000 rwxp 0003b000 03:01 786652 /lib/libsepol.so.1
00867000-00871000 rwxp 00867000 00:00 0
00873000-00888000 r-xp 00000000 03:01 787345 /lib/libnsl-2.5.so
00888000-00889000 r-xp 00014000 03:01 787345 /lib/libnsl-2.5.so
00889000-0088a000 rwxp 00015000 03:01 787345 /lib/libnsl-2.5.so
0088a000-0088c000 rwxp 0088a000 00:00 0
0088e000-00897000 r-xp 00000000 03:01 786504 /lib/libcrypt-2.5.so
00897000-00898000 r-xp 00008000 03:01 786504 /lib/libcrypt-2.5.so
00898000-00899000 rwxp 00009000 03:01 786504 /lib/libcrypt-2.5.so
00899000-008c0000 rwxp 00899000 00:00 0
008c2000-008cd000 r-xp 00000000 03:01 786647 /lib/libgcc_s-4.1.2-20080825.so.1
008cd000-008ce000 rwxp 0000a000 03:01 786647 /lib/libgcc_s-4.1.2-20080825.so.1
009e4000-00b0e000 r-xp 00000000 03:01 787740 /lib/libcrypto.so.0.9.8e
00b0e000-00b22000 rwxp 00129000 03:01 787740 /lib/libcrypto.so.0.9.8e
00b22000-00b25000 rwxp 00b22000 00:00 0
00b4c000-00b5d000 r-xp 00000000 03:01 787347 /lib/libresolv-2.5.so
00b5d000-00b5e000 r-xp 00010000 03:01 787347 /lib/libresolv-2.5.so
00b5e000-00b5f000 rwxp 00011000 03:01 787347 /lib/libresolv-2.5.so
00b5f000-00b61000 rwxp 00b5f000 00:00 0
00b73000-00b75000 r-xp 00000000 03:01 787348 /lib/libcom_err.so.2.1
00b75000-00b76000 rwxp 00001000 03:01 787348 /lib/libcom_err.so.2.1
00b7d000-00c11000 r-xp 00000000 03:01 3391542 /usr/lib/libkrb5.so.3.3
00c11000-00c14000 rwxp 00093000 03:01 3391542 /usr/lib/libkrb5.so.3.3
00c16000-00c18000 r-xp 00000000 03:01 787346 /lib/libkeyutils-1.2.so
00c18000-00c19000 rwxp 00001000 03:01 787346 /lib/libkeyutils-1.2.so
00c3b000-00c61000 r-xp 00000000 03:01 3391541 /usr/lib/libk5crypto.so.3.1
00c61000-00c62000 rwxp 00025000 03:01 3391541 /usr/lib/libk5crypto.so.3.1
00c64000-00c6c000 r-xp 00000000 03:01 3378836 /usr/lib/libkrb5support.so.0.1
00c6c000-00c6d000 rwxp 00007000 03:01 3378836 /usr/lib/libkrb5support.so.0.1
00c6f000-00c9c000 r-xp 00000000 03:01 3391543 /usr/lib/libgssapi_krb5.so.2.2
00c9c000-00c9d000 rwxp 0002d000 03:01 3391543 /usr/lib/libgssapi_krb5.so.2.2
00dc0000-00dc1000 r-xp 00dc0000 00:00 0 [vdso]
08048000-082cc000 r-xp 00000000 03:01 3872834 /root/saserver/1/gmsv/gmsvjt
082cc000-082fc000 rw-p 00283000 03:01 3872834 /root/saserver/1/gmsv/gmsvjt
082fc000-09e59000 rw-p 082fc000 00:00 0
0bc37000-0c22c000 rw-p 0bc37000 00:00 0 [heap]
40cc7000-413cf000 rw-p 40cc7000 00:00 0
413cf000-414d0000 rw-p 4b0f0000 00:00 0
414d0000-4b0f0000 rw-p 414d0000 00:00 0
4b130000-b7f6a000 rw-p 4b130000 00:00 0
b7f70000-b7f71000 rw-p b7f70000 00:00 0
bfe71000-bff48000 rw-p bff27000 00:00 0 [stack]
Aborted这样的错误,请大神真心能点一点我?我排查排疯了。。找不到原因,不是立刻报,是运行着过程中会需要运行到查数据库的,某一次就报了。到底为什么。。。
解决方案
String_Cat 中
if ((size2 + size1) < (s1_size - 1))//如果总和小于s1_len的长度
{
int i = 0;//
for (i = 0; i < size2; i++)
{
s1[size1 + i] = s2[i];
}
s1[size1 + i]='';//add
}
sasql_selectField_byWhereAndLimit 函数中 mysql_free_result(mysql_result);
//strcpysafe(_retstr sizeof(_retstr) result);
//free(result);
return result;//这个地方是错误的,平常可能会运行成功,但是当要明白的是result是一个局部变量且分配在栈空间,当函数调用完成之后,会释放掉 //会比较容易出错,建议定义一个全局变量,result 或者使用mallo分配堆空间(不建议这样会容易出现内存泄露哈) } else { mysql_free_result(mysql_result); return ""err""; }