Hi there,
using fpc 3.2.2 in ide lazarus 3.4 and today updated to 3.6 (no change in behaviour) under win 10 in a Virtualbox VM with enough free ram
I desperately want to create a global Database object which holds data in a key-value-store using a Tdictionary.
Later on i want to use a real database for persistent storage. but for now a TDict should do the job.
Now i have created a unit which defines a class TDatabase with a private KeyValueDict: TDictionary , its methods constructor create/get/store/destructor destroy/and instantiates a variable GlobalDatastore
But after "create" it seems, while stepping with the debugger, that the destructor is directly called afterwards which i dont understand.
is anything wrong with the method "create"?
Any access via "get" to KeyValueDict throws a read memory error. Compiling is fine but lots of warnings making me nervous.
Ive used the some AIs and the search function in this forum but could not find a solution
Ive added C:\lazarus\fpc\3.2.2\source\packages\rtl-generics\src to the path. Without this stepping into TDictionary was not possible.
Any Ideas?
I'm a total newbie to pascal but not for programming in other languages so its possible that ive done some pretty dumb mistakes.
Many thanks in advance.
unit TT_database;
// shared object
{$mode Delphi} {$H+}
interface
uses
Classes, SysUtils, Generics.Collections, Variants;
type
TDataBase = class
private
KeyValueDict: TDictionary<string, Variant>;
public
constructor Create;
destructor Destroy; override;
procedure Store(const Key: string; const Value: Variant);
function Get(const Key: string): Variant;
end;
var
GlobalDatastore: TDataBase;
implementation
{ TDataBase }
constructor TDataBase.Create;
begin
KeyValueDict := TDictionary<string, Variant>.Create;
end;
destructor TDataBase.Destroy;
begin
KeyValueDict.Free;
inherited;
end;
procedure TDataBase.Store(const Key: string; const Value: Variant);
begin
KeyValueDict.Add(Key, Value);
end;
function TDataBase.Get(const Key: string): Variant;
begin
if KeyValueDict.ContainsKey(Key) then
Result := KeyValueDict[Key]
else
Result := Null; // Verwende Null anstelle eines leeren Strings für Variant
end;
initialization
GlobalDatastore := TDataBase.Create;
finalization
GlobalDatastore.Destroy;
end.