BUG #5607: memmory leak in ecpg - Mailing list pgsql-bugs

From Marcelo Mas
Subject BUG #5607: memmory leak in ecpg
Date
Msg-id 201008061439.o76EdS1b003774@wwwmaster.postgresql.org
Whole thread Raw
Responses Re: BUG #5607: memmory leak in ecpg
List pgsql-bugs
The following bug has been logged online:

Bug reference:      5607
Logged by:          Marcelo Mas
Email address:      mmas@atg.com.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@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@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@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@server:~>

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

pgsql-bugs by date:

Previous
From: Tom Lane
Date:
Subject: Re: BUG #5606: DEFERRABLE and DEFERRABLE INITIALLY DEFERRED are the same
Next
From: Tom Lane
Date:
Subject: Re: BUG #5606: DEFERRABLE and DEFERRABLE INITIALLY DEFERRED are the same