在使用freetds時發現進行頻繁的連接,關閉資料庫時,出現內存持續增長.在網上搜索了一下,國內的文章就那幾篇,和我使用的方法一樣.最后在官方網站上找到了原因,寫出來,供大家參考使用,下面的代碼,是從網上找的,紅色部分是需要增加的.該示例只執行一次,隱藏了內存泄漏的問題.
Freetds的作者說dbinit,dbexit必須執行一次,再測試過程中執行多次也沒事.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sybfront.h>
#include <sybdb.h>
int main(void)
{
while(1)
{
char szUsername[32] = "test";
char szPassword[32] = "test";
char szDBName[32] = "test";
char szServer[32] = "2.2.2.2:1433";
//初始化db-library
dbinit();
//連接資料庫
LOGINREC *loginrec = dblogin();
DBSETLUSER(loginrec, szUsername);
DBSETLPWD(loginrec, szPassword);
DBPROCESS *dbprocess = dbopen(loginrec, szServer);
if(dbprocess == FAIL){
printf("ASB>> Conect MS SQL SERVER fail n");
return 0;
}else{
printf("ASB>> ConnectEMS conect MS SQL SERVER successn");
}
if(dbuse(dbprocess, szDBName) == FAIL){
printf("ASB>> Open database name failn");
}else{
printf("ASB>> Open database name successn");
}
//查詢資料庫
dbcmd(dbprocess, "select ID,BeginTime,Description from Alarms");
if(dbsqlexec(dbprocess) == FAIL){
printf("ASB>> Query Alarms table errorn");
}
DBINT result_code;
char szID[1024];
char szBeginTime[1024];
char szDescription[1024];
int rows = 0;
while ((result_code = dbresults(dbprocess)) != NO_MORE_RESULTS){
if (result_code == SUCCEED){
dbbind(dbprocess, 1, CHARBIND, (DBINT)0, (BYTE*)szID);
dbbind(dbprocess, 2, CHARBIND, (DBCHAR)0, (BYTE*)szBeginTime);
dbbind(dbprocess, 3, CHARBIND, (DBCHAR)0, (BYTE*)szDescription);
while (dbnextrow(dbprocess) != NO_MORE_ROWS){
printf("ASB>> ID=%sn", szID);
printf("ASB>> szAid=%sn", szBeginTime);
printf("ASB>> szBeginTime=%sn", szDescription);
}
}
}
//關閉資料庫連接
dbclose(dbprocess);
dbloginfree(loginrec);
dbexit();
usleep(1000);
}
return 0;
}
可任意轉載,但請保留出處,http://rainfish.cublog.cn/
[火星人 ] 關於freetds內存泄漏的問題已經有502次圍觀