GoogleCodeArchives / pyodbc

Automatically exported from code.google.com/p/pyodbc
MIT No Attribution
0 stars 0 forks source link

[patch] cursor.execute leaks parameters when exception is raised #264

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
cursor.execute leaks its parameters in most cases where an exception is raised.

This is very noticeable when updating a lot of data using the pattern:

try:
    cursor.execute('INSERT INTO table ...', params)
except pyodbc.IntegrityError:
    # row already exists; update it
    cursor.execute('UPDATE table ...', params)

This problem exists in 2.1.8, 3.0.5 and today's git head.

Comments in the code (cursor.cpp) suggest that this problem was considered, but 
deliberately left there for fear of inaccurate error reporting:

    if (!SQL_SUCCEEDED(ret) && ret != SQL_NEED_DATA && ret != SQL_NO_DATA)
    {
        // We could try dropping through the while and if below, but if there is an error, we need to raise it before
        // FreeParameterData calls more ODBC functions.
        return RaiseErrorFromHandle("SQLExecDirectW", cur->cnxn->hdbc, cur->hstmt);
    }

Attached is a proposed patch to resolve.

Original issue reported on code.google.com by lukedell...@gmail.com on 21 May 2012 at 12:07

Attachments:

GoogleCodeExporter commented 9 years ago
Fixed in 3.0.6-beta02

I just found this and fixed it myself while researching another problem.  I 
then found your report while continuing the research.  I could have saved 
myself a little time by checking out the patch first.

In my fix, I added the "free" call to the early exit.

Original comment by mkleehammer on 23 May 2012 at 8:06

GoogleCodeExporter commented 9 years ago
Thanks!  I do still like my patch better :-) see my review comments on your 
commit (revision 244e84a3e281):
http://code.google.com/p/pyodbc/source/detail?r=244e84a3e281f5f3387089ee49868a6f
b9ed1ff2

Original comment by lukedell...@gmail.com on 24 May 2012 at 12:15