Empty MemDataset crashing if FileName specified too early and file does not exist yet.
Original Reporter info from Mantis: wp @wpam
-
Reporter name:
Original Reporter info from Mantis: wp @wpam
- Reporter name:
Description:
TMemDatasset sometimes crashes when the filename is known too early and the file does not yet exist:
---------------------------------------
program memdbtest2;
{$mode objfpc}
uses
classes, sysutils, db,memds;
const
FILENAME = 'MemDB.dat';
var
MemDb: TMemDataset;
begin
// Make sure that data file does not exist.
if FileExists(FILENAME) then
DeleteFile(FILENAME);
MemDb := TMemDataset.Create(nil);
try
MemDb.FileName := FILENAME;
MemDb.FieldDefs.Add('Name', ftString, 20);
MemDb.CreateTable;
MemDb.Open; // <-- crash here if MemDb.FileName is set
MemDb.Append;
MemDb.Post;
finally
MemDb.Free;
end;
WriteLn('Done. Press ENTER to quit.');
ReadLn;
end.
---------------------------------------
This is because the method InternalOpen creates the file stream, FOpenStream, in fmOpenRead mode even if the file does not exist. Checking for existence of the file solves the issue here:
---------------------------------------
procedure TMemDataset.InternalOpen;
begin
If (FFileName<>'') and FileExists(FFileName) then
FOpenStream:=TFileStream.Create(FFileName,fmOpenRead);
...
--------------------------------------
Note that the obvious alternative to open the file in fmCreate mode crashes in the following InternalInitFieldDefs because it only checks whether the FOpenStream is nil.
Mantis conversion info:
- Mantis ID: 33446
- Version: 3.0.4
- Fixed in version: 3.1.1
- Fixed in revision: 38547 (#a795ff2e)
- Target version: 3.2.0