Delphi - Access violation while creating a TList of UnicodeString

旧巷老猫 提交于 2020-07-10 12:10:13

问题


I'm writing a Delphi package with RAD Studio XE7. I recently faced a strange access violation, and I cannot figure out why it happened. The context was that I was trying to maintain a list of font names. So I declared the following type:

ICustomFontList = TList<UnicodeString>;

Inside one of my classes, I declared a variable as follow:

m_pCustomFontList: ICustomFontList;

Then, in the constructor, I tried to instantiate this variable like that:

m_pCustomFontList := ICustomFontList.Create;

I compiled the package, and created a C++ project that used the code. However I gained a strange access violation every time the m_pCustomFontList was instantiated in the constructor, on the Begin line of the following code (located inside System.Generics.Collections.pas) :

constructor TList<T>.Create;
begin
    Create(TComparer<T>.Default);
end;

Later I found that a TStringList was a better choice for my values, and I changed to that type. From this moment, the above mentioned access violation never reappeared. However I cannot understand what the problem was. I create several TList (or derived like TObjectList) in my packages and I never faced a such problem with any other kind of TList I declared (i.e. other than TList of UnicodeString). So can anybody explain to me why this particular TList generated an access violation on construction? Theoretically, nothing prevent to create a TList of UnicodeString if it's my desire, or I'm wrong?

Regards

--- EDIT on the 15.02.2017

Here is a small example that reproduce the issue on my computer. For that a package project in Delphi should be created, and a C++ VCL forms application project that will use this package. In the package, create a new unit and copy the following code:

unit Unit1;

interface

uses System.Generics.Collections;

type
    ICustomFontList = TList<UnicodeString>;

    TaClass = class
        private
            m_pCustomFontList: ICustomFontList;

        public
            constructor Create; virtual;
    end;

implementation
constructor TaClass.Create;
begin
    inherited Create;

    m_pCustomFontList := ICustomFontList.Create;
end;

end.

In the c++ project, simply add this line in the constructor of TForm1:

std::auto_ptr<TaClass> paClass(new TaClass());

then build the package and run the project

Regards

来源:https://stackoverflow.com/questions/42231144/delphi-access-violation-while-creating-a-tlist-of-unicodestring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!