View Issue Details

IDProjectCategoryView StatusLast Update
0033444FPCDatabasepublic2018-03-17 12:54
ReporterwpAssigned ToMichael Van Canneyt 
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionfixed 
Product Version3.0.4Product Build 
Target Version3.2.0Fixed in Version3.1.1 
Summary0033444: TMemDataset crashing in Destroy
DescriptionUnder 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;
--------------------------------
TagsNo tags attached.
Fixed in Revision38540
FPCOldBugId
FPCTarget
Attached Files
  • memds.pp.patch (498 bytes)
    Index: fcl-db/src/memds/memds.pp
    ===================================================================
    --- fcl-db/src/memds/memds.pp	(revision 38538)
    +++ fcl-db/src/memds/memds.pp	(working copy)
    @@ -401,12 +401,13 @@
     
     destructor TMemDataset.Destroy;
     begin
    -  FStream.Free;
    +//  FStream.Free;
       FreeMem(FFieldOffsets);
       FreeMem(FFieldSizes);
       FBlobs.Clear;
       FBlobs.Free;
       inherited Destroy;
    +  FStream.Free;
     end;
     
     function TMemDataset.BookmarkValid(ABookmark: TBookmark): Boolean;
    
    memds.pp.patch (498 bytes)

Activities

wp

2018-03-17 00:42

reporter  

memds.pp.patch (498 bytes)
Index: fcl-db/src/memds/memds.pp
===================================================================
--- fcl-db/src/memds/memds.pp	(revision 38538)
+++ fcl-db/src/memds/memds.pp	(working copy)
@@ -401,12 +401,13 @@
 
 destructor TMemDataset.Destroy;
 begin
-  FStream.Free;
+//  FStream.Free;
   FreeMem(FFieldOffsets);
   FreeMem(FFieldSizes);
   FBlobs.Clear;
   FBlobs.Free;
   inherited Destroy;
+  FStream.Free;
 end;
 
 function TMemDataset.BookmarkValid(ABookmark: TBookmark): Boolean;
memds.pp.patch (498 bytes)

Michael Van Canneyt

2018-03-17 01:05

administrator   ~0107179

Applied, many thanks for the patch!

wp

2018-03-17 12:54

reporter   ~0107193

Thank you for this extremely fast fix.

Issue History

Date Modified Username Field Change
2018-03-17 00:41 wp New Issue
2018-03-17 00:42 wp File Added: memds.pp.patch
2018-03-17 01:03 Michael Van Canneyt Assigned To => Michael Van Canneyt
2018-03-17 01:03 Michael Van Canneyt Status new => assigned
2018-03-17 01:05 Michael Van Canneyt Fixed in Revision => 38540
2018-03-17 01:05 Michael Van Canneyt Note Added: 0107179
2018-03-17 01:05 Michael Van Canneyt Status assigned => resolved
2018-03-17 01:05 Michael Van Canneyt Fixed in Version => 3.1.1
2018-03-17 01:05 Michael Van Canneyt Resolution open => fixed
2018-03-17 01:05 Michael Van Canneyt Target Version => 3.2.0
2018-03-17 12:54 wp Note Added: 0107193
2018-03-17 12:54 wp Status resolved => closed