[SOLVED] hashlist with multiple specialization of TNullable lazarus / 62-General ---------------------------------------------------------------------- nomorelogic · 2022-01-10 08:36 hi all I've a FpHashList where I store multiple specializaed instances of TNullable using New and Dispose. Below you can see I store in the hashlist a specialized string and a specialized integer. Using Dispose I need to call Dispose with the correct typed pointer but - during the for loop - I don't know the type at the current position. Uncommentig "this code works" no exception is raised. How I can detect the correct pointer type to call Dispose? I need something like the "is" when comparing classes but for advanced records. thanks nomorelgoic 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 program test_hashlist_nullable; uses sysutils, nullable, Contnrs; procedure TestNullable;type  TNullString = specialize TNullable;  PNullString = ^TNullString;   TNullInteger= specialize TNullable;  PNullInteger = ^TNullInteger; var nullint: PNullInteger;    nullstr: PNullString;    i: integer;    HashList: TFPHashList;begin  WriteLn('Start');  HashList:=TFPHashList.Create;  try    // set values    nullstr:=new(PNullString);    nullstr^:='value 1';    HashList.Add('one', nullstr);     nullint:=new(PNullInteger);  nullint^:=2;    HashList.Add('two', nullint);     // write    WriteLn('one = ', PNullString(HashList.Find('one'))^.Value);    WriteLn('two = ', PNullInteger(HashList.Find('two'))^ .Value);      // free values    for i := 0 to HashList.Count -1 do begin        try        WriteLn('Dispose ' + i.ToString);          Dispose( PNullString(HashList[i]) );           { this code works          case i of            0: Dispose( PNullString(HashList[i]) );            1: Dispose( PNullInteger(HashList[i]) );          end;          }        except          on e: exception do begin             WriteLn('Error during dispose (' + i.ToString + '):' + e.Message);          end;      end;    end;   finally    // free resources    FreeAndNil(HashList);    WriteLn('Stop');  end;end;  begin  TestNullable;end.  Edit: fixed a message in code ---------------------------------------------------------------------- Thaddy · 2022-01-10 10:11 Well, rtti might help a little here. Although with generics you can't - possibly easily -retrieve the distinct type name, of the specialization,the crc value is distinct for any speciailzation. Here you can see that s2 has the same type as s and that n2 has the same type as n. Consider: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 {$mode obfpc}uses nullable,typinfo;type  TNullableInteger = specialize TNullable;  TNullableString = specialize TNullable;var  Info:PTypeInfo;  n,n2:TNullableInteger;  s,s2:TNullableString;begin  Info := TypeInfo(n);  writeln(info^.name);  Info := TypeInfo(s);  writeln(info^.name);    Info := TypeInfo(n2);  writeln(info^.name);    Info := TypeInfo(s2);  writeln(info^.name);  end.But this may very well be implementation detail. I will see if I can get at the typeinfo for TNullable.value. If that is possible you get tkInteger and tkString. Currenty experimenting with TrttiContext from rtti.pp ---------------------------------------------------------------------- nomorelogic · 2022-01-10 10:34 Quote from: Thaddy on January 10, 2022, 11:11:54 amI will see if I can get at the typeinfo for TNullable.value. If that is possible you get tkInteger and tkString. I think this is surely a better way for performances ---------------------------------------------------------------------- Thaddy · 2022-01-10 12:27 Must have been sleeping. It is really easy: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 {$mode obfpc}uses nullable,typinfo;type  TNullableInteger = specialize TNullable;  TNullableString = specialize TNullable;var  Info:PTypeInfo;  n,n2:TNullableInteger;  s,s2:TNullableString;begin  Info := TypeInfo(n.value);  writeln(info^.kind);  Info := TypeInfo(s.value);  writeln(info^.kind);    Info := TypeInfo(n2.value);  writeln(info^.kind);    Info := TypeInfo(s2.value);  writeln(info^.kind);  end.So to determin the concrete type, simply query the typeinfo for TNullable.Value. ---------------------------------------------------------------------- PascalDragon · 2022-01-10 12:29 Quote from: Thaddy on January 10, 2022, 11:11:54 amWell, rtti might help a little here. Although with generics you can't - possibly easily -retrieve the distinct type name, of the specialization,the crc value is distinct for any speciailzation. RTTI won't help here, because all that nomorelogic has is a pointer. And neither the pointer nor what it points to has any type information by itself. Quote from: Thaddy on January 10, 2022, 01:27:03 pmSo to determin the concrete type, simply query the typeinfo for TNullable.Value. That is nonsense, because nomorelogic would need to cast to a specialized TNullable<> first and then the type information would be whatever it was casted to. Quote from: nomorelogic on January 10, 2022, 09:36:27 amHow I can detect the correct pointer type to call Dispose? I need something like the "is" when comparing classes but for advanced records. There is no safe way to do this. You additionally need to store what kind of type you're dealing with. You need to do this anyway if you want to use the values, because there you'd have to guess as well otherwise. ---------------------------------------------------------------------- engkin · 2022-01-10 13:31 For the simple example you have, the compiler emits FreeMem for Dispose (to be more precise it calls fpc_freemem). You can, in this specific case, replace Dispose with FreeMem: 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           //Dispose( PNullString(HashList[i]) );          Freemem(HashList[i]); In the general case, this may not work, the compiler generates a node for Dispose statements Dispose ==> in_dispose_x ==> new_dispose_statement(is_new:boolean) : tnode; Take a look at function new_dispose_statement(is_new:boolean) in unit pinline.pas ---------------------------------------------------------------------- nomorelogic · 2022-01-10 15:00 thank you all for suggestions @Thaddy and @PascalDragon yes the question is I only have a pointer and no other info anyway what I need is: instead of this Code: [Select]var i: integer begin   i := 5;   writeln('integer I =', i); end; I want to write this Code: [Select]var ms: TMemoryspace; begin   ms:=TMemoryspace.Create;   // property TMemoryspace.Integers wraps TNullableIntegers using Getter and Setter   ms.Integers['i'] := 5;   writeln('integer I =',ms.Integers['i']);   ms.Free; end; thus I know what type I need to have a value but don't know at dispose time @engkin yes, your suggestion about use FreeMem works following code works fine 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 program test_hashlist_nullable; uses sysutils, nullable, Contnrs; procedure TestNullable;type  TNullString = specialize TNullable;  PNullString = ^TNullString;   TNullInteger= specialize TNullable;  PNullInteger = ^TNullInteger; var nullint: PNullInteger;    nullstr: PNullString;    i: integer;    HashList: TFPHashList;begin  WriteLn('Start');  HashList:=TFPHashList.Create;  try    // set values    nullstr:=new(PNullString);    nullstr^:='value 1';    HashList.Add('one', nullstr);     nullint:=new(PNullInteger);  nullint^:=2;    HashList.Add('two', nullint);     // write    WriteLn('one = ', PNullString(HashList.Find('one'))^.Value);    WriteLn('two = ', PNullInteger(HashList.Find('two'))^ .Value);      // free values    for i := 0 to HashList.Count -1 do begin        try        WriteLn('Dispose ' + i.ToString);          // Dispose( PNullString(HashList[i]) );          FreeMem(HashList[i]);        except          on e: exception do begin             WriteLn('Error during sispose (' + i.ToString + '):' + e.Message);          end;      end;    end;   finally    // free resources    FreeAndNil(HashList);    WriteLn('Stop');  end;end;  begin  TestNullable;end.  anyway I think I'm going to store the type in order to have "schema" thank you all again nomorelogic