freetds AT lists.ibiblio.org
Subject: FreeTDS Development Group
List archive
- From: Craig Davison <cd AT securityfocus.com>
- To: freetds AT lists.ibiblio.org
- Subject: [freetds] ODBC memory leak?
- Date: Wed, 5 Mar 2003 23:46:48 -0700
Hi again,
I discovered a memory leak in the application I'm developing, and I think
I may have traced the problem to either the Freetds ODBC driver or
unixODBC. Of course, it's possible I'm not explicity freeing something
that I should.
Sample code that compiles on my RedHat 8 (x86) machine is attached
(change the connection parameters in main() to try it out). Here's what
I'm doing, in pseudocode:
SQLAllocHandle(SQL_NULL_HANDLE,hEnv)
SQLAllocHandle(hEnv,hDbc)
SQLConnect(hDbc,...)
Loop:
SQLAllocHandle(hDbc,hStmt)
SQLBindCol(hStmt,...)
SQLExecDirect(hStmt,...)
SQLFetch(hStmt) . . . .
SQLFreeStmt(hStmt)
SQLFreeHandle(hStmt)
SQLDisconnect(hDbc)
SQLFreeHandle(hDbc)
SQLFreeHandle(hEnv)
The inner loop seems to leave 8 k unfreed ever time it is run.
To see the memory leak, run top or something alongside it and watch the
process grow by 8 k per second.
BTW my server is SQLServer 2000, and I'm using unixODBC 2.2.2.
--
Craig Davison
Symantec Corporation
+1 (403) 213-3939 ext. 228
#include <stdio.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
int TryQuery (SQLHDBC hOdbc)
{
SQLINTEGER SQLid;
SQLINTEGER SQLerr;
long erg;
SQLHSTMT hStmt;
char pQuery[501];
SQLAllocHandle(SQL_HANDLE_STMT, hOdbc, &hStmt);
sprintf (pQuery, "SELECT uid FROM master..sysusers WHERE uid=0");
printf ("query: %s\n", pQuery);
SQLBindCol(hStmt, 1, SQL_C_SLONG, &SQLid, sizeof(SQLINTEGER),
&SQLerr);
erg = SQLExecDirect (hStmt, pQuery, SQL_NTS);
if (erg != SQL_SUCCESS)
{
printf ("ERROR\n");
SQLFreeHandle(SQL_HANDLE_STMT, &hStmt);
return 0;
}
erg = SQLFetch (hStmt);
while (erg == SQL_SUCCESS)
{
printf ("Data: %d\n", (int)SQLid);
erg = SQLFetch (hStmt);
}
if (erg != SQL_NO_DATA)
{
printf ("ERROR\n");
SQLFreeStmt (hStmt, SQL_CLOSE);
SQLFreeHandle(SQL_HANDLE_STMT, &hStmt);
return 0;
}
SQLFreeStmt (hStmt, SQL_CLOSE);
SQLFreeHandle(SQL_HANDLE_STMT, &hStmt);
return 1;
}
int main (int argc, char **argv)
{
SQLHENV hEnv;
SQLHDBC hOdbc;
long erg;
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv);
SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hOdbc);
/* connect to the database */
erg = SQLConnect(hOdbc, (SQLCHAR*) "server", SQL_NTS,
(SQLCHAR*) "user", SQL_NTS,
(SQLCHAR*) "pass", SQL_NTS);
if (erg != SQL_SUCCESS)
{
printf ("Database Connect Error\n");
SQLFreeHandle(SQL_HANDLE_DBC, &hOdbc);
SQLFreeHandle(SQL_HANDLE_ENV, &hEnv);
return 0;
}
while (TryQuery (hOdbc))
sleep (1);
SQLDisconnect(hOdbc);
SQLFreeHandle(SQL_HANDLE_DBC, &hOdbc);
SQLFreeHandle(SQL_HANDLE_ENV, &hEnv);
return 0;
}
-
[freetds] ODBC memory leak?,
Craig Davison, 03/06/2003
- Re: [freetds] ODBC memory leak?, James K. Lowden, 03/06/2003
- <Possible follow-up(s)>
-
RE: [freetds] ODBC memory leak?,
ZIGLIO Frediano, 03/06/2003
-
Re: [freetds] ODBC memory leak?,
Nick Gorham, 03/06/2003
- Re: [freetds] ODBC memory leak?, Craig Davison, 03/06/2003
- Re: [freetds] ODBC memory leak?, Craig Davison, 03/06/2003
-
Re: [freetds] ODBC memory leak?,
Nick Gorham, 03/06/2003
- RE: [freetds] ODBC memory leak?, ZIGLIO Frediano, 03/07/2003
Archive powered by MHonArc 2.6.24.