[freetds] cursor changes and ref count

Frediano Ziglio freddyz77 at tin.it
Fri Aug 4 07:19:36 EDT 2006

  just to let someone know yesterday I changed a bit how cursors are
handled. Mainly I added reference counting to cursors. The reason was
that for some tests (cursors tests under Sybase) causes memory access
after free (not that good). The problem was that there are a lot of
references to cursors:
- cur_cursor in TDSSOCKET, to maintain current cursor for storing
results, handle close and similar
- cursors in TDSSOCKET to maintain list of cursors allocated in server
and handle possible deallocation
- client cursor to make possible to client library to reference a cursor
So I did these changes
- remove tds_free_cursor, free is handled internally by src/tds/mem.c
- added a tds_release_cursor. This function decrease reference counter
(ref_count member) and if zero free cursor. Note that when you need to
add a new reference you need to increment ref_count. Currently
tds_cursor_alloc take into account client pointer and you should not
retain pointer after calling tds_cursor_dealloc. tds_cursor_close
instead do not decrement counter even if it send deallocation!
- added a tds_cursor_dealloted to remove cursor from TDSSOCKET->cursors.
This function should only be called by libTDS when it detect a cursor
deallocation from server. Currently cursors are put in
TDSSOCKET->cursors linked list just after allocation, I think I'll
cahnge this, TDSSOCKET->cursors should be used only for server cursors.
Also libTDS should be able to detect that client have no more pointers
to cursors and free it when possible (that is when connection is idle).
I think that in current code is possible that an application that keep
connection open and use extensively cursors lead to some leaks for this

I also noted that we have a current_results and a reference counter in
TDSRESULTINFO... I think that it would be a good idea to use the
reference counter also for this pointer (just to reminds this reference
counter was added to support safely row buffering in dblib where each
cached row keep a pointer to resultinfo).

Well, so the state of cursors and odbc is now
- it do not cause memory problems (just fixed)
- if seems to work on mssql but more testing are required and updates do
not works (current code in query.c is able just to make a test happy...)
- sybase does not work


More information about the FreeTDS mailing list