BUG #5607: memmory leak in ecpg

From: "Marcelo Mas" <mmas(at)atg(dot)com(dot)uy>
To: pgsql-bugs(at)postgresql(dot)org
Subject: BUG #5607: memmory leak in ecpg
Date: 2010-08-06 14:39:28
Message-ID: 201008061439.o76EdS1b003774@wwwmaster.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-hackers


The following bug has been logged online:

Bug reference: 5607
Logged by: Marcelo Mas
Email address: mmas(at)atg(dot)com(dot)uy
PostgreSQL version: 8.4.4
Operating system: Suse 10
Description: memmory leak in ecpg
Details:

Valgrind reports memmory leak when getting decimal data.

We created a small testcase that reproduces the problem that:

1) crates a table whith one decimal attribute
2) inserts one row.
3) executes a exec sql sentence to retrieve data from database.
4) displays data
5) dropps the table
6) disconnects
7) exits

When running with valgrind, it reports that memmory obtained in point 3)
is not liberated.

Next follows contents of files :
decimaltest.ec whith source code,
compiletest to compile ,
and runtest to run it with valgrind,

After files contents, follows terminal output.

------ BEGIN OF decimaltest.ec --------------
#include <stdio.h>
#include <stdlib.h>
#include "pgtypes_numeric.h"

int show_error(char * message){
if( sqlca.sqlcode != 0 ) {
printf("Error %s: [%d] \n",message , sqlca.sqlcode );
printf("\nsqlcaid = %s \n", sqlca.sqlcaid );
printf("sqlabc = %ld \n", sqlca.sqlabc );
printf("sqlcode = %ld \n", sqlca.sqlcode );
printf("sqlerrm.sqlerrml = %d \n",
sqlca.sqlerrm.sqlerrml );
printf("sqlerrm.sqlerrmc = %s \n",
sqlca.sqlerrm.sqlerrmc );
printf("sqlerrp = %s \n", sqlca.sqlerrp );
printf("sqlerrd = %d \n", sqlca.sqlerrd );
printf("sqlwarn = %s \n", sqlca.sqlwarn );
printf("sqlstate = %s \n\n", sqlca.sqlstate );
return(-1);
}
return(0);
}

int main(int argc, char *argv[]) {
EXEC SQL BEGIN DECLARE SECTION ;
char base_datos[100];
char usuario[100];
char contrasenia[100];
decimal valor;

char sentence[200];

EXEC SQL END DECLARE SECTION;
int count = 0;

if( argc < 4) {
printf("use : %s <database(at)server> <username> <password>\n", argv[0]
);
return(-1);
}


/*--------------------------------------------------------------------------
--
/ connect to database

/---------------------------------------------------------------------------
-*/
strcpy( base_datos, argv[1] );
strcpy( usuario, argv[2] );
strcpy( contrasenia, argv[3] );
EXEC SQL CONNECT TO :base_datos USER :usuario / :contrasenia ;

if( sqlca.sqlcode != 0 ) {
printf("Error Connecting:[%d] \n", sqlca.sqlcode );
return(-1);
}

printf("Connected\n" );

sprintf(sentence, "create table testdecimal ( tdecimal
decimal(18,9));");
EXEC SQL EXECUTE IMMEDIATE :sentence;
if ( show_error("creting table ")!= 0 ) return(-1);
sprintf(sentence, "insert into testdecimal values ( '12345654.32101234'
);");
EXEC SQL EXECUTE IMMEDIATE :sentence;
if ( show_error("inserting data ")!= 0 ) return(-1);

/**********/
/* TEST */
/**********/
EXEC SQL
SELECT tdecimal into :valor from testdecimal limit 1;

if ( show_error("obtaining data ")!= 0 ) return(-1);

printf("ndigits = %d \n", valor.ndigits);
printf("weitht = %d \n", valor.weight);
printf("rscale = %d \n", valor.rscale);
printf("dscale = %d \n", valor.dscale);
printf("sign = %d \n", valor.sign);
printf("ndigits = ");
for ( count = 0 ; count< valor.ndigits ; count++ ){
printf("%02x ",valor.digits[count]);
}

printf("\n\n");

sprintf(sentence, "drop table testdecimal;");
EXEC SQL EXECUTE IMMEDIATE :sentence;
if ( show_error("dropping table ")!= 0 ) return(-1);

EXEC SQL DISCONNECT CURRENT;
return(0);

}
------ END OF decimaltest.ec --------------

-------BEGIN OF compiletest ---------------
POSTGRE_DIR=/opt/PostgreSQL/8.4
POSTGRE_INCLUDE=$POSTGRE_DIR/include
POSTGRE_LIB=$POSTGRE_DIR/lib/
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$POSTGRE_DIR/lib
$POSTGRE_DIR/bin/ecpg decimaltest.ec
gcc -g decimaltest.c -I$POSTGRE_INCLUDE -L$POSTGRE_LIB -lecpg -o
decimaltest.exe

------ END OF compiletest ---------------

------ BEGIN OF runtest--------------------
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/PostgreSQL/8.4/lib
valgrind --tool=memcheck --leak-check=full --leak-resolution=high
./decimaltest.exe testdatabase(at)server psig psig
------ END OF runtest--------------------

------ BEGIN OF terminal output------------
==9642== Memcheck, a memory error detector
==9642== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==9642== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==9642== Command: ./decimaltest.exe prurmmas(at)pruebas2:5433 psig psig
==9642==
Connected
ndigits = 17
weitht = 7
rscale = 9
dscale = 9
sign = 0
ndigits = 01 02 03 04 05 06 05 04 03 02 01 00 01 02 03 04 00

==9642==
==9642== HEAP SUMMARY:
==9642== in use at exit: 239 bytes in 2 blocks
==9642== total heap usage: 128 allocs, 126 frees, 42,698 bytes allocated
==9642==
==9642== 19 bytes in 1 blocks are definitely lost in loss record 1 of 2
==9642== at 0x401CE85: calloc (vg_replace_malloc.c:418)
==9642== by 0x4223299: pgtypes_alloc (in
/opt/PostgreSQL/8.4/lib/libpgtypes.so.3.1)
==9642== by 0x4220583: alloc_var (in
/opt/PostgreSQL/8.4/lib/libpgtypes.so.3.1)
==9642== by 0x4220886: PGTYPESnumeric_from_asc (in
/opt/PostgreSQL/8.4/lib/libpgtypes.so.3.1)
==9642== by 0x4027911: ecpg_get_data (in
/opt/PostgreSQL/8.4/lib/libecpg.so.6.1)
==9642== by 0x4023B93: ecpg_store_result (in
/opt/PostgreSQL/8.4/lib/libecpg.so.6.1)
==9642== by 0x4025D49: ECPGdo (in
/opt/PostgreSQL/8.4/lib/libecpg.so.6.1)
==9642== by 0x804893C: main (decimaltest.ec:71)
==9642==
==9642== LEAK SUMMARY:
==9642== definitely lost: 19 bytes in 1 blocks
==9642== indirectly lost: 0 bytes in 0 blocks
==9642== possibly lost: 0 bytes in 0 blocks
==9642== still reachable: 220 bytes in 1 blocks
==9642== suppressed: 0 bytes in 0 blocks
==9642== Reachable blocks (those to which a pointer was found) are not
shown.
==9642== To see them, rerun with: --leak-check=full --show-reachable=yes
==9642==
==9642== For counts of detected and suppressed errors, rerun with: -v
==9642== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 43 from 8)
psig(at)server:~>

------ END OF terminal output------------

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Frank Heikens 2010-08-06 14:50:01 Re: BUG #5606: DEFERRABLE and DEFERRABLE INITIALLY DEFERRED are the same
Previous Message Tom Lane 2010-08-06 14:14:28 Re: BUG #5606: DEFERRABLE and DEFERRABLE INITIALLY DEFERRED are the same

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2010-08-06 14:53:06 Re: default of max_stack_depth
Previous Message Kevin Grittner 2010-08-06 14:31:13 Re: CommitFest 2010-07 week three progress report