Menu

#19 One MORE Remove method bug :)

open
HashMap (7)
7
2003-10-05
2003-10-05
No

It seems that /at least/ TStrHashMap.Remove doesn't
free memory occupied by removed object, even if
FOwnsObjects is TRUE.

Quick hack to fix that is to replace following code in
TStrHashMap.Remove:

if not FOwnsObjects then
Result := Bucket.Entries[I].Value;
System.Move(Bucket.Entries[I + 1], Bucket.Entries
[I],
(Bucket.Count - I) * SizeOf(TStrEntry));
Dec(Bucket.Count);
Exit;

with this one:

Result := Bucket.Entries[I].Value;
System.Move(Bucket.Entries[I + 1], Bucket.Entries
[I],
(Bucket.Count - I) * SizeOf(TStrEntry));
Dec(Bucket.Count);
if FOwnsObjects then
FreeAndNil(Result);
Exit;

This solves a problem, methink. Also it seems all other
HashMap objects has this problem too, however I didn't
look further in.

Discussion

  • Jean-Philippe BEMPEL

    • priority: 5 --> 7
    • assigned_to: nobody --> rdm_30
     
  • Jean-Philippe BEMPEL

    Logged In: YES
    user_id=294266

    same for THashMap.remove

    I will fix with:

    if not FOwnsObjects then
    Result := Bucket.Entries[I].Value
    else
    Bucket.Entries[I].Value.Free;
    System.Move(Bucket.Entries[I + 1], Bucket.Entries[I],
    Bucket.Count - I) * SizeOf(TStrEntry));

     

Log in to post a comment.

MongoDB Logo MongoDB