Nick Gorham | 1 Mar 2007 17:02
Favicon

Another test (sorry)

And again.

Normal service will be resumed.

--

-- 
Nick Gorham
Easysoft Limited
http://www.easysoft.com, http://www.unixODBC.org

_______________________________________________
unixODBC-dev mailing list
unixODBC-dev <at> easysoft.com
http://mail.easysoft.com/mailman/listinfo/unixodbc-dev

Siddarth Sharma | 6 Mar 2007 07:11

Memory Leak - SQLDriverConnect()

Hi All,

 

Below is the code of my sample application.

 

main() {

    SQLHENV env;

    SQLHDBC dbc;

    SQLHSTMT stmt;

    SQLRETURN ret; /* ODBC API return status */

    SQLCHAR outstr[1024] = {0};

    SQLSMALLINT outstrlen;

    unsigned long loop = 0;

 

    /* Allocate an environment handle */

    SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);

 

    /* We want ODBC 3 support */

    SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);

 

    /* Allocate a connection handle */

    SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);

 

    /* Connect */

    ret = SQLDriverConnect(dbc, NULL, "SERVERNAME=127.0.0.1;DSN=xsw;DATABASE=xsw;UID=xsw",

            strlen("SERVERNAME=127.0.0.1;DSN=xsw;DATABASE=xsw;UID=xsw"),

            outstr, sizeof(outstr), &outstrlen, SQL_DRIVER_NOPROMPT);

 

    if (SQL_SUCCEEDED(ret)) {

        printf("Connected\n");

        printf("Returned connection string was:\n\t%s\n", outstr);

 

        if (ret == SQL_SUCCESS_WITH_INFO) {

           printf("Driver reported the following diagnostics\n");

        }

 

        SQLDisconnect(dbc);       /* disconnect from driver */

     } else {

         fprintf(stderr, "Failed to connect\n");

     }

 

    /* free up allocated handles */

    SQLFreeHandle(SQL_HANDLE_DBC, dbc);

    SQLFreeHandle(SQL_HANDLE_ENV, env);

 

    return 0;

}

 

 

When I run this application (with connection pooling enabled) with valgrind then it shows some memory leaks in SQLDriverConnect().

Below is the snippet of the valgrind output where it shows memory leaks.

 

==2862== 32,840 (80 direct, 32,760 indirect) bytes in 1 blocks are definitely lost in loss record 45 of 69

==2862==    at 0x401B85E: malloc (vg_replace_malloc.c:149)

==2862==    by 0x409C159: __gconv_open (in /lib/tls/libc.so.6)

==2862==    by 0x409BDF4: iconv_open (in /lib/tls/libc.so.6)

==2862==    by 0x406AF70: unicode_setup (__info.c:534)

==2862==    by 0x403268D: __connect_part_one (SQLConnect.c:971)

==2862==    by 0x40379C8: SQLDriverConnect (SQLDriverConnect.c:1118)

==2862==    by 0x8048629: main (test.c:24)

==2862==

==2862==

==2862== 32,840 (80 direct, 32,760 indirect) bytes in 1 blocks are definitely lost in loss record 48 of 69

==2862==    at 0x401B85E: malloc (vg_replace_malloc.c:149)

==2862==    by 0x409C159: __gconv_open (in /lib/tls/libc.so.6)

==2862==    by 0x409BDF4: iconv_open (in /lib/tls/libc.so.6)

==2862==    by 0x406AF91: unicode_setup (__info.c:535)

==2862==    by 0x403268D: __connect_part_one (SQLConnect.c:971)

==2862==    by 0x40379C8: SQLDriverConnect (SQLDriverConnect.c:1118)

==2862==    by 0x8048629: main (test.c:24)

 

Seems to me that iconv_close() is not getting called when I disconnect using SQLDisconnect().

 

Am I doing something wrong or is this a problem with unix odbc library?

 

Thanks & Regards,

Siddarth

 

PS: I am connecting to Postgres DB

_______________________________________________
unixODBC-dev mailing list
unixODBC-dev <at> easysoft.com
http://mail.easysoft.com/mailman/listinfo/unixodbc-dev
Nick Gorham | 6 Mar 2007 11:04
Favicon

Re: Memory Leak - SQLDriverConnect()

Siddarth Sharma wrote:

>Hi All,
>
> 
>
>Below is the code of my sample application.
>  
>

Unless someone can think of a better way, thats how it is at the moment, 
Pooling involves saving a global list of connections, as I don't know if 
another connection is going to be requested, there isn't a place to free 
the list. If the leak is a problem, than all I can suggest, is disable 
pooling in the DM, and handle it in the app, assuming the app is aware 
when its finished with the connections.

--

-- 
Nick Gorham
Easysoft Limited
http://www.easysoft.com, http://www.unixODBC.org

_______________________________________________
unixODBC-dev mailing list
unixODBC-dev <at> easysoft.com
http://mail.easysoft.com/mailman/listinfo/unixodbc-dev

Tom Hughes | 6 Mar 2007 11:28
Gravatar

Re: Memory Leak - SQLDriverConnect()

In message <45ED3C9F.5010704 <at> easysoft.com>
        Nick Gorham <nick.gorham <at> easysoft.com> wrote:

> Siddarth Sharma wrote:
>
>>Hi All,
>>
>> Below is the code of my sample application.
>>
>
> Unless someone can think of a better way, thats how it is at the
> moment, Pooling involves saving a global list of connections, as I
> don't know if another connection is going to be requested, there isn't
> a place to free the list. If the leak is a problem, than all I can
> suggest, is disable pooling in the DM, and handle it in the app,
> assuming the app is aware when its finished with the connections.

If there is a global list somewhere which is still holding that iconv
handle then valgrind will not (or at least should not) report it as a
leak.

For valgrind to report something as definitely lost there must be no
way to reach it from any local or global variables - ie from the
registers, stack or data/bss segments.

Speaking with my valgrind developer's hat on, if it is misreporting
a leak then I'd like to know about it...

Tom

--

-- 
Tom Hughes (thh <at> cyberscience.com)
Software Engineer, Cyberscience Corporation
http://www.cyberscience.com/
_______________________________________________
unixODBC-dev mailing list
unixODBC-dev <at> easysoft.com
http://mail.easysoft.com/mailman/listinfo/unixodbc-dev

Siddarth Sharma | 6 Mar 2007 11:55

RE: Memory Leak - SQLDriverConnect()


I don't think that valgrind is mis-reporting here.

I think the problem here is that unicode_shutdown() is not getting called at
all. And if I am not wrong then it is the responsibility of unicode_shutdown
to do all the iconv cleanup business.

Regards,
Siddarth

-----Original Message-----
From: unixodbc-dev-bounces <at> easysoft.com
[mailto:unixodbc-dev-bounces <at> easysoft.com] On Behalf Of Tom Hughes
Sent: Tuesday, March 06, 2007 3:59 PM
To: unixodbc-dev <at> easysoft.com
Subject: Re: [unixODBC-dev] Memory Leak - SQLDriverConnect()

In message <45ED3C9F.5010704 <at> easysoft.com>
        Nick Gorham <nick.gorham <at> easysoft.com> wrote:

> Siddarth Sharma wrote:
>
>>Hi All,
>>
>> Below is the code of my sample application.
>>
>
> Unless someone can think of a better way, thats how it is at the
> moment, Pooling involves saving a global list of connections, as I
> don't know if another connection is going to be requested, there isn't
> a place to free the list. If the leak is a problem, than all I can
> suggest, is disable pooling in the DM, and handle it in the app,
> assuming the app is aware when its finished with the connections.

If there is a global list somewhere which is still holding that iconv
handle then valgrind will not (or at least should not) report it as a
leak.

For valgrind to report something as definitely lost there must be no
way to reach it from any local or global variables - ie from the
registers, stack or data/bss segments.

Speaking with my valgrind developer's hat on, if it is misreporting
a leak then I'd like to know about it...

Tom

--

-- 
Tom Hughes (thh <at> cyberscience.com)
Software Engineer, Cyberscience Corporation
http://www.cyberscience.com/
_______________________________________________
unixODBC-dev mailing list
unixODBC-dev <at> easysoft.com
http://mail.easysoft.com/mailman/listinfo/unixodbc-dev

_______________________________________________
unixODBC-dev mailing list
unixODBC-dev <at> easysoft.com
http://mail.easysoft.com/mailman/listinfo/unixodbc-dev

Nick Gorham | 6 Mar 2007 11:59
Favicon

Re: Memory Leak - SQLDriverConnect()

Tom Hughes wrote:

>Speaking with my valgrind developer's hat on, if it is misreporting
>a leak then I'd like to know about it...
>
>Tom
>
>  
>
Ok, given that, I will look a bit further :-)

--

-- 
Nick
_______________________________________________
unixODBC-dev mailing list
unixODBC-dev <at> easysoft.com
http://mail.easysoft.com/mailman/listinfo/unixodbc-dev

Nick Gorham | 6 Mar 2007 12:01
Favicon

Re: Memory Leak - SQLDriverConnect()

Siddarth Sharma wrote:

>I don't think that valgrind is mis-reporting here.
>
>I think the problem here is that unicode_shutdown() is not getting called at
>all. And if I am not wrong then it is the responsibility of unicode_shutdown
>to do all the iconv cleanup business.
>
>  
>
Well, feel free to suggest a fix, failing that, I will take a look at it 
when I have a moment.

Its related to pooling in some way, as it doesn't happen if you don't 
use pooling.

--

-- 
Nick Gorham
Easysoft Limited
http://www.easysoft.com, http://www.unixODBC.org

_______________________________________________
unixODBC-dev mailing list
unixODBC-dev <at> easysoft.com
http://mail.easysoft.com/mailman/listinfo/unixodbc-dev

Tom Hughes | 6 Mar 2007 12:02
Gravatar

Re: Memory Leak - SQLDriverConnect()

In message <007301c75fde$02db8db0$9d2d10ac <at> synapse.com>
        Siddarth Sharma <siddarth.sharma <at> globallogic.com> wrote:

> I don't think that valgrind is mis-reporting here.
>
> I think the problem here is that unicode_shutdown() is not getting called at
> all. And if I am not wrong then it is the responsibility of unicode_shutdown
> to do all the iconv cleanup business.

I agree I think.

Something must have free the connection handle that the iconv handle
was attached to, or valgrind would have found a pointer to the iconv
handle in the connection structure.

That something should presumably have called unicode_shutdown() before
it freed the connection handle.

Tom

--

-- 
Tom Hughes (thh <at> cyberscience.com)
Software Engineer, Cyberscience Corporation
http://www.cyberscience.com/
_______________________________________________
unixODBC-dev mailing list
unixODBC-dev <at> easysoft.com
http://mail.easysoft.com/mailman/listinfo/unixodbc-dev

Nick Gorham | 7 Mar 2007 23:59
Favicon

Re: Memory Leak - SQLDriverConnect()

Tom Hughes wrote:

>In message <007301c75fde$02db8db0$9d2d10ac <at> synapse.com>
>        Siddarth Sharma <siddarth.sharma <at> globallogic.com> wrote:
>
>  
>
>>I don't think that valgrind is mis-reporting here.
>>
>>I think the problem here is that unicode_shutdown() is not getting called at
>>all. And if I am not wrong then it is the responsibility of unicode_shutdown
>>to do all the iconv cleanup business.
>>    
>>
>
>I agree I think.
>
>Something must have free the connection handle that the iconv handle
>was attached to, or valgrind would have found a pointer to the iconv
>handle in the connection structure.
>
>That something should presumably have called unicode_shutdown() before
>it freed the connection handle.
>
>Tom
>
>  
>
Ok, found and fixed the problem, valgrind was right, the connection 
information when being copied into the pooled list wasn't taking the 
iconv handles over. there is a 2.2.13pre on the ftp site with the fix in.

However, unicode_shutdown is still noting to do with it, its still not 
being called, as the connection is still in the poling list at the end 
of execution. The reason valgrind was flagging it was the info was being 
lost, not being copied over. The connection handle never gets free'd

--

-- 
Nick
_______________________________________________
unixODBC-dev mailing list
unixODBC-dev <at> easysoft.com
http://mail.easysoft.com/mailman/listinfo/unixodbc-dev

Tom Hughes | 8 Mar 2007 00:56
Gravatar

Re: Memory Leak - SQLDriverConnect()

In message <45EF43B5.4070406 <at> easysoft.com>
          Nick Gorham <nick.gorham <at> easysoft.com> wrote:

> However, unicode_shutdown is still noting to do with it, its still not
> being called, as the connection is still in the poling list at the end
> of execution. The reason valgrind was flagging it was the info was being
> lost, not being copied over. The connection handle never gets free'd

That's fine if the handle winds up attached to a connection in the
pool that will still be around at process exit.

I was just guessing (incorrectly as it turns out) at where the problem
might lie.

Tom

--

-- 
Tom Hughes (thh <at> cyberscience.com)
Software Engineer, Cyberscience Corporation
http://www.cyberscience.com/
_______________________________________________
unixODBC-dev mailing list
unixODBC-dev <at> easysoft.com
http://mail.easysoft.com/mailman/listinfo/unixodbc-dev


Gmane