[unixODBC-support] Memory leak in SQLFetch?

Craddock, Richard C cmi5 at cdc.gov
Mon Dec 13 18:27:11 GMT 2004


Ladies and Gentlemen:

 

I have a reproducible memory leak with the attached code.  There are
exactly 20160 elements that should be returned from the query.  After
running this program there is nearly 3Mb of un-freed memory.  If I
comment out the SQLFetch() calls then the memory leak goes away.  I
suspect that it may have something to do with how I am using unixODBC
rather then a memory leak in unixODBC.  But I have stared at the code
for quite sometime to no avail.  Has anyone had a similar problem?  Have
I made a mistake somewhere?  I appreciate your time and assistance.

 

Regards,

Cameron Craddock

 

 

#include <stdio.h>

#include <sql.h>

#include <sqlext.h>

#include <sqltypes.h>

 


int main( int argc, char *argv[] )

{

 


    SQLHENV V_OD_Env;

    SQLHDBC V_OD_hdbc;

 


    char        V_OD_stat[ 10 ];

    SQLSMALLINT V_OD_mlen;

    char        V_OD_msg[ 200 ];

    long        V_OD_erg;

    SQLINTEGER  V_OD_err;

 


    char        dsn[] = "database_dsn";

    char        username[] = "database_user";

    char        password[] = "database_password";


 


    char clonename[255];

 


    // SQL statement handle

    SQLHSTMT    queryHstmt;

 


    // allocate Environment handle and register version

    V_OD_erg = SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE,
&V_OD_Env );

 


    if(( V_OD_erg != SQL_SUCCESS ) && ( V_OD_erg !=
SQL_SUCCESS_WITH_INFO ))

    {

        printf( "Error AllocHandle\n");

        return( -1 );

    }

 


    V_OD_erg = SQLSetEnvAttr( V_OD_Env, SQL_ATTR_ODBC_VERSION,
(void*)SQL_OV_ODBC3, 0 );

 


    if(( V_OD_erg != SQL_SUCCESS ) && ( V_OD_erg !=
SQL_SUCCESS_WITH_INFO ))

    {

        printf("Error SetEnv\n" );

        SQLFreeHandle( SQL_HANDLE_ENV, V_OD_Env );

        return( -1 );

    }

 


    // allocate connection handle, set timeout

    V_OD_erg = SQLAllocHandle( SQL_HANDLE_DBC, V_OD_Env, &V_OD_hdbc );

 


    if(( V_OD_erg != SQL_SUCCESS ) && ( V_OD_erg != SQL_SUCCESS ))

    {

        printf("Error AllocHDB\n");

        SQLFreeHandle( SQL_HANDLE_ENV, V_OD_Env );

        return( -1 );

    }

 


    SQLSetConnectAttr( V_OD_hdbc, SQL_LOGIN_TIMEOUT, ( SQLPOINTER *)5,
0);

 


    // connect to the datasource

    V_OD_erg = SQLConnect( V_OD_hdbc, ( SQLCHAR* )dsn, SQL_NTS,

        ( SQLCHAR* )username, SQL_NTS, ( SQLCHAR* )password, SQL_NTS );

 


    if(( V_OD_erg != SQL_SUCCESS ) && ( V_OD_erg !=
SQL_SUCCESS_WITH_INFO ))

    {

        printf("Error SQLConnect\n");

        SQLGetDiagRec( SQL_HANDLE_DBC, V_OD_hdbc, 1, ( SQLCHAR*
)V_OD_stat, &V_OD_err,

            ( SQLCHAR* )V_OD_msg, 100, &V_OD_mlen );

        SQLFreeHandle( SQL_HANDLE_DBC, V_OD_hdbc );

        return( -1 );

    }

 


    // allocate a statement handle for this sql query

    V_OD_erg = SQLAllocHandle( SQL_HANDLE_STMT, V_OD_hdbc, &queryHstmt
);

 


    if(( V_OD_erg != SQL_SUCCESS ) && ( V_OD_erg !=
SQL_SUCCESS_WITH_INFO ))

    {

        printf("Error in AllocStatement\n");

        SQLGetDiagRec( SQL_HANDLE_DBC, V_OD_hdbc, 1, ( SQLCHAR* )
V_OD_stat, &V_OD_err,

            ( SQLCHAR* )V_OD_msg, 100, &V_OD_mlen );

        return( -1 );

    }

 


    // Bind cloneName to the 1st column of the result

    V_OD_erg = SQLBindCol( queryHstmt, 1, SQL_C_CHAR, clonename, 150,
&V_OD_erg );

    if(( V_OD_erg != SQL_SUCCESS ) && ( V_OD_erg !=
SQL_SUCCESS_WITH_INFO ))

    {

        printf("Error in SQLBindCol\n");

        SQLGetDiagRec( SQL_HANDLE_DBC, V_OD_hdbc, 1, ( SQLCHAR*
)V_OD_stat, &V_OD_err,

            ( SQLCHAR* )V_OD_msg, 100, &V_OD_mlen );

        SQLFreeHandle( SQL_HANDLE_STMT, queryHstmt );

        return( -1 );

    }

 


    // execute the sql statement

    V_OD_erg = SQLExecDirect( queryHstmt,

        "Select ps.Clone from printsetsAV ps WHERE ps.printset_iid =
63", SQL_NTS );

 


    if(( V_OD_erg != SQL_SUCCESS ) && ( V_OD_erg !=
SQL_SUCCESS_WITH_INFO ))

    {

        printf("Error in SQLExec\n");

        SQLGetDiagRec( SQL_HANDLE_DBC, V_OD_hdbc, 1, ( SQLCHAR*
)V_OD_stat, &V_OD_err,

            ( SQLCHAR* )V_OD_msg, 100, &V_OD_mlen );

        SQLFreeHandle( SQL_HANDLE_STMT, queryHstmt );

        return( -1 );

    }

 


    // fetch the first result of the sql statement

    V_OD_erg = SQLFetch( queryHstmt );

 


    // loop until there are no more results

    while( V_OD_erg != SQL_NO_DATA )

    {

        V_OD_erg = SQLFetch( queryHstmt );

    }

 


    SQLFreeStmt(queryHstmt, SQL_CLOSE );

    SQLFreeHandle( SQL_HANDLE_STMT, queryHstmt );

    SQLDisconnect( V_OD_hdbc );

    SQLFreeHandle( SQL_HANDLE_DBC, V_OD_hdbc );

    SQLFreeHandle( SQL_HANDLE_ENV, V_OD_Env );

    return( 1 );

}

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.unixodbc.org/pipermail/unixodbc-support/attachments/20041213/dcf016b7/attachment.html>


More information about the unixODBC-support mailing list