Issue 264: [patch] cursor.execute leaks parameters when exception is raised
Status:  Complete
Owner: ----
Closed:  May 2012
Reported by lukedell...@gmail.com, May 20, 2012
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.

pyodbc-memleak.patch
4.0 KB   View   Download
May 23, 2012
Project Member #1 mkleehammer
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.
Status: Complete
May 23, 2012
#2 lukedell...@gmail.com
Thanks!  I do still like my patch better :-) see my review comments on your commit (revision 244e84a3e281):
https://code.google.com/p/pyodbc/source/detail?r=244e84a3e281f5f3387089ee49868a6fb9ed1ff2