gerald-lindsly / mongo-delphi-driver

MongoDB Driver for Delphi
Apache License 2.0
47 stars 26 forks source link

TMongoCursor.Free Corrupts My Interface to the MongoDB #4

Closed KarlHahn closed 11 years ago

KarlHahn commented 11 years ago

I am using the 2012-6-28 version of the mongo-delphi-driver compiled for x86. I am having an issue with using the TMongoCursor type. The cursor object performs as expected until the point where I free it. Once I've freed a TMongoCursor, every query I make to the database subsequent to that throws an exception from ntdll.dll. If I comment out the cursor.free, the problem goes away, although I fear that creates a memory leak. Here is some sample code:

function ListAnonCases(database: TMongo; ns: string): TStringList; var slist: TStringList; query: TBson; cursor: TMongoCursor; b: TBson; s: string; begin

slist := TStringList.Create; try
query := bsonEmpty; cursor := TMongoCursor.Create(query); // cursor.sort := BSON([dbfAnonymized, true]); if database.find(ns, cursor) then begin while cursor.next do begin b := cursor.value(); s := b.value(dbfAnonymized); slist.Add(s); // b.free; end; end; cursor.free; except on E: exception do Main.LogException('ListAnonCases', 0, E); end; result := slist; end;

Note that this function itself does not throw any exception, but the next attempt to query the database does. The next access is a TMongo.findone that has worked many times prior to executing the above code. But it throws the Read Access Violation @ 00000000 when I do it after executing the above. Again, commenting out the cursor.free line makes the problem go away. The problem is unaffected by commenting out or leaving in the b.free or enabling the sort.

gerald-lindsly commented 11 years ago

You caught me. I just pushed a patch to fix this. Apologies.

KarlHahn commented 11 years ago

Verified your fix. Works fine, lasts a long time. Many thanks.

gerald-lindsly commented 11 years ago

Pardon me for closing the issue before you responded. I jumped the gun there. But, you're welcome. I hope the driver and MongoDB work out well for you.