Recent

Author Topic: TFPGMap - does it free objects ?  (Read 2212 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1710
Re: TFPGMap - does it free objects ?
« Reply #15 on: October 27, 2025, 05:31:28 pm »
Quote
I have to turn your examples into compilable code every time.

I wish I could make a compilable program that pin-points the exact cause of any problem. 

The reality is, I have tested my own codes bit by bit for several days, assuming that there would be no errors in the Lazarus itself.
Finally, because I coundn't find any problem in my codes, I came to test the behaviors of mapping objects, which didn't behave as I expected.

Now I can make compilable codes, but I did not know what to include and not from the whole codes of mine. But before, I did not know the source of the problem.  I was curious whether the assumptions I had were correct.

Anyway many thanks to the developers of FPC/Lazarus. I'm making money with my application developed with it (hope to donate someday) :D



Thaddy

  • Hero Member
  • *****
  • Posts: 18515
  • Here stood a man who saw the Elbe and jumped it.
Re: TFPGMap - does it free objects ?
« Reply #16 on: October 27, 2025, 05:42:24 pm »
You can safely assume there are no errors in Lazarus or FPC itself. (There are, but not related to your problem.)
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

egsuh

  • Hero Member
  • *****
  • Posts: 1710
Re: TFPGMap - does it free objects ?
« Reply #17 on: October 29, 2025, 03:12:20 am »
Please test this.

Pressing button 1, and closing the application causes no problem.

But Button 2 causes problem.

If I do not use AddList, then no problem.

If the key is integer type, AddList does not cause any problem.

sfeinst

  • Sr. Member
  • ****
  • Posts: 250
Re: TFPGMap - does it free objects ?
« Reply #18 on: October 29, 2025, 02:43:05 pm »
I don't know if this plays into it or not.  But I looked at TFPGMapObject online docs.  There's no AddList.  It inherites from TFPSMap, also no Addlist.  It inherits from TFPSList.  This has AddList.

So I have to wonder, if none of the Map type objects overrride AddList, how are they keys maintained when you call AddList (which a list object would not know about)?  It is possible something within the source is calling some other overridden method.  Just basing this on what I see in online docs.

As I said, no idea if this is why you are running into issues.  But if the keys are not being maintained, I'm not sure AddList is what you want to use.

cpicanco

  • Hero Member
  • *****
  • Posts: 674
  • Behavioral Scientist and Programmer
    • Portfolio
Re: TFPGMap - does it free objects ?
« Reply #19 on: October 29, 2025, 02:55:11 pm »
Just want to mention that I once had a problem with TFPGMap that was workedaround using Generics.Collection's TDictionary:

https://forum.lazarus.freepascal.org/index.php/topic,65269.msg497243.html#msg497243
Be mindful and excellent with each other.
https://github.com/cpicanco/

egsuh

  • Hero Member
  • *****
  • Posts: 1710
Re: TFPGMap - does it free objects ?
« Reply #20 on: October 30, 2025, 03:22:36 am »
Quote
But I looked at TFPGMapObject online docs.  There's no AddList.  It inherites from TFPSMap, also no Addlist.  It inherits from TFPSList.  This has AddList.

Yes, I checked the online document. So, even TFPGMap.AddList does not work correctly, if your reasoning is correct.
There should be TFPSMap.AddList method, overiding TFPSList.AddList (this is not virtual).

But the real issue is, even when I did not use AddList method, Access Violation happened when I reordered the list using TFPGMapObject and terminated application. But I cannot reproduce it in the example I attached.

I'll check my program again (or try to reproduce it).

Thausand

  • Sr. Member
  • ****
  • Posts: 444
Re: TFPGMap - does it free objects ?
« Reply #21 on: October 30, 2025, 03:35:16 am »
I don't know if this plays into it or not.  But I looked at TFPGMapObject online docs.  There's no AddList.  It inherites from TFPSMap, also no Addlist.  It inherits from TFPSList.  This has AddList.
Addlist make call copyitems, copyitems make call copyitem, copyitem is overload TFPGMapObject. Is work  :)

@egsuh
When make example call Foo addlist:
Code: Pascal  [Select][+][-]
  1. Foo.AddList(Bar)
then Bar not own object when create Bar else error if Free/Destroy Foo.

You code not do Bar := TBar.Create(false);

Then Bar TData is free two time. if addlist bar to other Foo then Bar TData is free three time ....

egsuh

  • Hero Member
  • *****
  • Posts: 1710
Re: TFPGMap - does it free objects ?
« Reply #22 on: October 30, 2025, 04:17:56 am »
@Thausand

Thank you for your comment. I see. CopyItem is overrided. Anyway AddList causes problem.

My issue is not freeing object twice. I'm using two TFPGMap, and one TFPGMapObject, and they share the same object. What I want is simply reordering. So, TFPGMapObject should free the objects, while TFPGMap does not. All operations are executed correctly. Simply there are Access Violation WHEN I TERMINATE THE APPLICATION.     

Anyway I got some inspirations from these discussions, and I re-arranged my structure as following:
 
Code: Pascal  [Select][+][-]
  1.     type
  2.        TaqVarList = specialize TFPGMap<string, TaqVar>;
  3.        TaqVarObjectList = specialize TFPGMapObject<string, TaqVar>;
  4.      
  5.     var
  6.        AVar: TaqVar;
  7.  
  8.        QVarList : TaqVarList;
  9.        VarList : TaqVarObjectList;
  10.        OrderedVarList: TaqVarList;
  11.      
  12.     begin
  13.        QVarList := TaqVarList.Create;
  14.        VarList := TaqVarObjectList.Create;  // instead of TaqVarList.Create;
  15.        OrderedVarList:= TaqVarList.Create;  // instead of TaqVarObjectList.Create;
  16.        
  17.         for ti := 0 to N do begin
  18.              for tj := 1 to N2 [ti] do begin
  19.                   avar := TaqVar.Create;
  20.                   QVarList.Add(IntToStr(ti * N + tj), avar);
  21.              end;
  22.    
  23.             for tj := 0 to QVarList.count-1 do VarList.Add(QVarList.Keys[tj], QVarList.Data[tj];
  24.             // instead of  VarList.AddList(QVarList);
  25.    
  26.             QVarList.Clear;
  27.         end;  
  28.      
  29.          for i := 0 to VarLIst.Count - 1 do
  30.               OrderedVarList.Add(NewOrderStr[i], VarList.Data[i]);
  31.    
  32.          qvarlist.free;
  33.          varlist.free;
  34.          orderedvarlist.free;
  35.     end ;


This does not cause any problem.

Thausand

  • Sr. Member
  • ****
  • Posts: 444
Re: TFPGMap - does it free objects ?
« Reply #23 on: October 30, 2025, 04:34:58 am »
@egsuh
thank for reply.

I think I not understand.

Example post #17 is work and no error for button1 and no error for button2. Then I not know what is you example error.

I have compile fpc 3.2.2 -glh -Criot.

egsuh

  • Hero Member
  • *****
  • Posts: 1710
Re: TFPGMap - does it free objects ?
« Reply #24 on: October 30, 2025, 05:01:34 am »
Quote
Example post #17 is work and no error for button1 and no error for button2. Then I not know what is you example error.

Do not set HeapTrk. Run the program, press Button2, and the Close the program. The error comes at the last moment.

Thausand

  • Sr. Member
  • ****
  • Posts: 444
Re: TFPGMap - does it free objects ?
« Reply #25 on: October 30, 2025, 05:16:39 am »
Do not set HeapTrk. Run the program, press Button2, and the Close the program. The error comes at the last moment.
Thank you egsuh. Now I have error  :D

Then need more investigate...

jamie

  • Hero Member
  • *****
  • Posts: 7402
Re: TFPGMap - does it free objects ?
« Reply #26 on: October 30, 2025, 11:19:52 pm »
I can't tell you anything because I am stupid, However, I did download your test app you presented first and was able to get Button2 code to work just fine.

 I used ShortStrings instead of manage strings.

Code: Pascal  [Select][+][-]
  1.  TMySHortstring=String[20];
  2.    TMap = specialize TFPGMap<integer, TAClass>;
  3.    TMapObject = specialize TFPGMapObject<integer, TAClass>;
  4.  
  5.    TSMap = specialize TFPGMap<TMyShortString, TAClass>;
  6.    TSMapObject = specialize TFPGMapObject<TMyShortString, TAClass>;
  7.                                                                          
  8.  

Jamie
The only true wisdom is knowing you know nothing

Thausand

  • Sr. Member
  • ****
  • Posts: 444
Re: TFPGMap - does it free objects ?
« Reply #27 on: October 31, 2025, 01:17:09 am »
That is  interest Jamie.

Then is maybe problem reference string. Is make small logic. Difficult for solve when use ansistring and is better what have write Thaddy for owned.

The problem is easier to remedy with rtl-generics, because that let's you choose if the keys and/or the values are owned.

jamie

  • Hero Member
  • *****
  • Posts: 7402
Re: TFPGMap - does it free objects ?
« Reply #28 on: October 31, 2025, 01:40:37 am »
Could be a Reference count not set to zero in the managed String pool.

But I use strings like that, and I don't get that problem, must be where the moon is today.

Jamie
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018