[unixODBC-dev] Memory leak in unixODBC?

Matthias Meier meier at fuh.de
Fri Sep 3 15:42:23 BST 2004


  - or is it my programming fault?

Hello!

Im pretty new to [unix]ODBC so I wrote a simple function which connects to
an ODBC (Informix) database an selects one colum in one row.
If I put this function in a loop for 100 selects
I start with an program-memory size of 2.7 MB and end up with 8 MB!!

(Yes, i know, opening and closing the connection isn't really good but it should work without loosing any memory)

Any ideas? -thaks for help!



Here is the code:
-----------------

int run( void )
{
         SQLHENV         hEnv;   // Handle ODBC environment
         SQLHDBC         hDbc;   // Handle connection
         SQLHSTMT        hStmt;  // Handle for a statement
         long            rc;     // result of functions
         SQLINTEGER      err;
         char            buffer[500];

         // 1. allocate Environment handle and register version
         rc=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&hEnv);

         rc=SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);

         // 2. allocate connection handle, set timeout
         rc = SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDbc);

         // 3. Connect to the datasource
         rc = SQLConnect(        hDbc,
                                 (SQLCHAR*) "bmo", SQL_NTS,
                                 (SQLCHAR*) "", SQL_NTS,
                                 (SQLCHAR*) "", SQL_NTS);

         // 4. alloc statement
         rc=SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStmt);

         // 5. execute
         rc=SQLExecDirect(hStmt,"SELECT kname1 FROM kunde0 where kkey = 2000 ",SQL_NTS);

         // 6. Bind
         rc=SQLBindCol( hStmt, 1, SQL_C_CHAR, buffer, 200, &err );

         rc=SQLFetch(hStmt);
         while(rc != SQL_NO_DATA)
         {
                 printf("Result: %s\n", buffer );
                 rc=SQLFetch(hStmt);
         };

         // 7. Cleanup
         SQLFreeHandle(SQL_HANDLE_STMT,hStmt);
         SQLDisconnect(hDbc);
         SQLFreeHandle(SQL_HANDLE_DBC,hDbc);
         SQLFreeHandle(SQL_HANDLE_ENV, hEnv);

         return( 0 );
}


void main( void )
{
         int ii;
         for( ii=1; ii <= 100; ii++)
         {
                 fprintf( stderr, "---------------------------------- %d\n", ii );
                 run();
         };
}






More information about the unixODBC-dev mailing list