Newbie problem with TDictionary lazarus / 67-Windows ---------------------------------------------------------------------- phunkz · 2024-11-02 22:40 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. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit TT_database;// shared object {$mode Delphi}  {$H+} interface uses  Classes, SysUtils, Generics.Collections, Variants; type  TDataBase = class  private    KeyValueDict: TDictionary;  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.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 Variantend; initialization  GlobalDatastore := TDataBase.Create; finalization  GlobalDatastore.Destroy; end. ---------------------------------------------------------------------- silvercoder70 · 2024-11-03 01:59 Hi, I have used your code exactly as is and it actually worked for me and behind a button put code ... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  s: string;begin  GlobalDatastore.Store('a', '1');   s := GlobalDatastore.Get('a');  showMessage(s);end;     Please keep in mind I am using the 4.0RC1 and FPC 3.2.2. As a side-note I did set breakpoints in all of your functions/methods as well. What versions are you using? ---------------------------------------------------------------------- Thaddy · 2024-11-03 07:04 Your code is a bit, well, over the top for what you actually mean. Maybe you can do it like this:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit mydatab;{$mode delphi}interfaceuses  Generics.Collections; // that is all you need type  TYourDatabase = TDictionary; var  GlobalDatastore: TYourDatabase; implementation initialization  GlobalDatastore := TYourDatabase.Create; finalization  GlobalDatastore.Free; end. I would not call your data store TDatabase, since that is an existing and often used class. I also would not use store and get if all they do is calling add and retrieve, so I removed that. Above unit is tested and working. ---------------------------------------------------------------------- phunkz · 2024-11-04 01:40 Thanks to both, working on your tipps, especially on the naming conventions, i found the error, really dumb as previously announced... I had an identically named local variable in the calling unit...