TMemDataset crashing in Destroy
Original Reporter info from Mantis: wp @wpam
-
Reporter name:
Original Reporter info from Mantis: wp @wpam
- Reporter name:
Description:
Under some circumstances, I observed TMemDataset crashing at the end of a program if
- the dataset was not explicitly closed
- a filename was specified in the corresponding property.
This is a simple test program which crashes in the call to Free:
--------------------------
program memdbtest;
{$mode objfpc}
uses
classes, db,memds;
var
MemDb: TMemDataset;
begin
MemDb := TMemDataset.Create(nil);
try
MemDb.FieldDefs.Add('Name', ftString, 20);
MemDb.CreateTable;
MemDb.Open;
MemDb.Append;
MemDb.FieldByName('Name').Value := 'Test';
MemDb.Post;
MemDb.FileName := 'MemDB.txt';
finally
MemDb.Free; // <--- crash here
end;
WriteLn('Done. Press ENTER to quit.');
ReadLn;
end.
--------------------------
The crash happens because the first instruction of TMemDataset.Destroy frees the internal stream, FStream. But in InternalClose which is called by the inherited Destroy the MemDS tries to write to file if the FileName is not empty and fir this purpose the FStream is needed again.
The crash can be avoided if "FStream.Free" is called at the end of the destructor:
-------------------------------
destructor TMemDataset.Destroy;
begin
FreeMem(FFieldOffsets);
FreeMem(FFieldSizes);
FBlobs.Clear;
FBlobs.Free;
inherited Destroy;
FStream.Free; // moved to here from first line
end;
--------------------------------
Mantis conversion info:
- Mantis ID: 33444
- Version: 3.0.4
- Fixed in version: 3.1.1
- Fixed in revision: 38540 (#185979e0)
- Target version: 3.2.0