John Kouraklis 94 Posted August 28, 2020 Hi, I've got difficulties figuring out what is wrong with the following code: program Project1; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, System.Generics.Collections; var dic: TObjectDictionary<string, TList<string>>; list: TList<string>; begin try dic:=TObjectDictionary<string, TList<string>>.Create([doOwnsValues]); list:=TList<string>.Create; list.Add('111'); dic.AddOrSetValue('aaa', list); var gg: integer:=dic.Items['aaa'].Count; writeln(gg); // 1 var l2: TList<string>:=dic.Items['aaa']; var a:integer:=l2.Count; writeln(a); // 1 l2.Add('222'); a:=l2.Count; writeln(a); // 2 dic.Items['aaa']:=l2; a:=l2.Count; writeln(a); // 0??? a:=dic.Items['aaa'].count; writeln(a); // 0??? dic.Free; // Invalid point? readln; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end. When I assign the TList to the dictionary Value, it loses all the elements. What am I doing wrong here? Can't see what is going on. Thanks Share this post Link to post
Henk Post 3 Posted August 28, 2020 The dic[‘aaa’] and l2 are the same object already. Hence, the assignment is not needed at all. But the direct assignment (formerly known as “AddOrSetValue” in this case will set the value. This is done by removing (and destroying) the old value and after that add the new one... Thus the l2 is “cleared” (destroyed). Share this post Link to post
pyscripter 694 Posted August 28, 2020 (edited) dic.Items['aaa']:=l2; dic.Items['aaa'] already contains l2 and l2 is freed before it is readded, since you set doOwnsValues Also an issue would occur if you do dic.Items['bbb']:=l2; When the dictionary is destroyed the list will be freed twice. Edited August 28, 2020 by pyscripter Share this post Link to post
John Kouraklis 94 Posted August 28, 2020 So, the doOwnsValues frees the objects in assignments as well. I thought it only takes care of them when the dictionary is freed. Thanks a lot guys Share this post Link to post
David Heffernan 2353 Posted August 28, 2020 Also the owned objects are destroyed when items are removed from the dictionary. This is the same ownership model as the classic TObjectList. 1 Share this post Link to post
Stefan Glienke 2019 Posted August 28, 2020 That's why IMultiMap<TKey,TValue> from Spring4d is so cool 3 Share this post Link to post
David Heffernan 2353 Posted August 28, 2020 16 minutes ago, Stefan Glienke said: That's why IMultiMap<TKey,TValue> from Spring4d is so cool Only cool for the users. Mind-bending if it's your job to implement it! 1 1 Share this post Link to post
John Kouraklis 94 Posted August 28, 2020 4 hours ago, Stefan Glienke said: That's why IMultiMap<TKey,TValue> from Spring4d is so cool So, how does it behave? never used it Share this post Link to post
Stefan Glienke 2019 Posted August 28, 2020 What do you mean how does it behave - like a multimap Share this post Link to post
John Kouraklis 94 Posted August 30, 2020 Oh, good to know it honors its name Share this post Link to post