pg.ddx.io  pgsql-odbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Kelly Burkhart <kelly@tradebotsystems.com>
To: Peter Eisentraut <peter_e@gmx.net>
Cc: pgsql-odbc@postgresql.org
Subject: Re: Memory Leak when freeing statement
Date: Wed, 30 Mar 2005 14:39:49 -0600
Message-ID: <1112215189.3392.70.camel@krb06> (raw)
In-Reply-To: <200503302006.38606.peter_e@gmx.net>
References: <1111690332.26601.206.camel@krb06>
	<200503302006.38606.peter_e@gmx.net>

On Wed, 2005-03-30 at 12:06, Peter Eisentraut wrote:
> Kelly Burkhart wrote:
> > I have found a memory leak when freeing a statement handle which has
> > had parameters bound in psqlodbc-08.00.0101.  I was able to eliminate
> > the leak by adding:
> >
> >         SC_free_params(self, STMT_FREE_PARAMS_ALL);
> >
> > to line 330 of statement.c in the SC_Destructor method.  I'm not
> > familiar enough with the code to know if this is a sensible fix.
> 
> Can you provide an example program that shows the memory leak?

In the attached program, each query iteration leaks 12 bytes per input
variable.  Output variables have no effect.

-K

Attachments:

tst_odbcleak.cpp (text/x-c++, 3.5K) download #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sql.h> #include <sqlext.h> #define CONN_DB ((SQLCHAR*)"<DB>") #define CONN_USR ((SQLCHAR*)"<USR>") #define CONN_PWD ((SQLCHAR*)"<PWD>") static void ckSts( RETCODE sts, const char *method ) { if (sts != SQL_SUCCESS && sts != SQL_SUCCESS_WITH_INFO) { fprintf( stderr, "%s failed with status: %d\n", method, sts ); exit(1); } } static void query( HDBC hdbc ); int main() { HENV henv = SQL_NULL_HENV; HDBC hdbc = SQL_NULL_HDBC; int32_t version = SQL_OV_ODBC3; RETCODE status; status = SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv ); ckSts( status, "SQLAllocHandle(ENV)" ); status = SQLSetEnvAttr( henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER *)version, 0 ); ckSts( status, "SQLSetEnvAttr" ); status = SQLAllocHandle( SQL_HANDLE_DBC, henv, &hdbc ); ckSts( status, "SQLAllocHandle(DBC)" ); status = SQLConnect( hdbc, CONN_DB, SQL_NTS, CONN_USR, SQL_NTS, CONN_PWD, SQL_NTS ); ckSts( status, "SQLConnect" ); { int idx; for( idx=0; idx<1000; ++idx ) query( hdbc ); } SQLDisconnect( hdbc ); SQLFreeHandle( SQL_HANDLE_DBC, hdbc ); SQLFreeHandle( SQL_HANDLE_ENV, henv ); return 0; } static void query( HDBC hdbc ) { HSTMT hstmt; /* Each time I add an input variable I lose 12 bytes per iteration */ char input0[1024]; SDWORD iind0 = sizeof(input0); char input1[1024]; SDWORD iind1 = sizeof(input1); char input2[1024]; SDWORD iind2 = sizeof(input1); /* Adding output vars doesn't affect leak */ char output0[1024]; SDWORD oind0; char output1[1024]; SDWORD oind1; char statement[1024]; RETCODE status; /* strcpy( statement, "select 'foo', 'bar' where 'abcdefg' = ?" ); /**/ /* strcpy( statement, "select 'foo', 'bar' where 'abcdefg' = ? and 'abcdefg' = ?" ); /**/ strcpy( statement, "select 'foo', 'bar' where 'abcdefg' = ? and 'abcdefg' = ? and 'abcdefg' = ?" ); /**/ strcpy( input0, "abcdefg" ); strcpy( input1, "abcdefg" ); strcpy( input2, "abcdefg" ); SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ); status = SQLPrepare( hstmt, (unsigned char *)statement, SQL_NTS ); ckSts( status, "SQLPrepare" ); status = SQLBindParameter( hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, sizeof(input0), 0, (UCHAR *)input0, 0, (SDWORD *)&iind0 ); ckSts( status, "SQLBindParameter" ); status = SQLBindParameter( hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, sizeof(input1), 0, (UCHAR *)input1, 0, (SDWORD *)&iind1 ); ckSts( status, "SQLBindParameter" ); status = SQLBindParameter( hstmt, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, sizeof(input2), 0, (UCHAR *)input2, 0, (SDWORD *)&iind2 ); ckSts( status, "SQLBindParameter" ); status = SQLBindCol( hstmt, 1, SQL_C_CHAR, (UCHAR *)output0, sizeof(output0), (SDWORD *)&oind0 ); ckSts( status, "SQLBindCol" ); status = SQLBindCol( hstmt, 2, SQL_C_CHAR, (UCHAR *)output1, sizeof(output1), (SDWORD *)&oind1 ); ckSts( status, "SQLBindCol" ); status = SQLExecute( hstmt ); ckSts( status, "SQLExecute" ); status = SQLFetch( hstmt ); ckSts( status, "SQLFetch" ); printf("Fetched \"%s\", \"%s\"\n", output0, output1 ); SQLFreeHandle( SQL_HANDLE_STMT, hstmt ); }

view thread (3+ messages)

Message-ID: <1112215189.3392.70.camel@krb06>
Permalink:  ../1112215189.3392.70.camel@krb06/
Also on:    postgresql.org/message-id/1112215189.3392.70.camel@krb06

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-odbc@postgresql.org
  Cc: kelly@tradebotsystems.com, peter_e@gmx.net
  Subject: Re: Memory Leak when freeing statement
  In-Reply-To: <1112215189.3392.70.camel@krb06>
  References: <1111690332.26601.206.camel@krb06> <200503302006.38606.peter_e@gmx.net> <1112215189.3392.70.camel@krb06>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox