GetItem on TDictionary eleminated by linker

2019-05-23 11:22发布

问题:

I am using a TDictionary of <string, string>. But for some reason, the linker decides that I do not want to get items out of it.

I have the following code:

function TSheet.GetFieldName(Field: string; Default: string): string;
begin
  Result := Default;
  if FFieldNames[Field] = '' then
    Result := Field
  else
    Result := FFieldNames[Field];
end;

FFieldNames is a TDictionary<string, string>. On line 2 (if FFieldNames[Field] = '' then), it throws a 'File not found' exception. Adding FFieldNames[Field] to my watch tells me that Function to be called, {System.Generics.Collections}TDictionary.GetItem, was eliminated by linker.

Someone asked here on a similar issue on how to avoid the linker eliminating functions during debugging. From this I gathered, that the compiler/linker assumes that I am not using it. Someone suggested - during conversation - that I should try using it more.

So I created the following code:

FFieldNames.Add(Name, S);
V := FFieldNames.Items[Name];

Where S, Name and V are strings. This is from the code where FFieldNames is filled with data. V's only purpose is to obtain the just inserted S; it does nothing else.

Strangely, while the debugger tells me the same thing (i.e. GetItem being eliminated), V does get set to the expected value. But it does not in my TSheet.GetFieldName function. :|

What am I missing?

回答1:

The same problem applies to TList<>. Even if the code is using a method in the class it is not accessible from the debugger ("xxx on TList eliminated by linker"). I guess this is a problem with generics in general.

If you make a descendent class it will not have this problem

type
  TMyList = class(TList<TMyObject>)

  end;

var
  List : TMyList;
begin
  ...

end;