为什么这个程序会报告内存泄漏?
{$APPTYPE CONSOLE}
uses
System.Generics.Collections;
type
TDerivedGenericObjectList = class(TObjectList<TObject>)
public
constructor Create;
end;
constructor TDerivedGenericObjectList.Create;
begin
inherited;
end;
var
List: TDerivedGenericObjectList;
begin
ReportMemoryLeaksOnShutdown := True;
List := TDerivedGenericObjectList.Create;
List.Add(TObject.Create);
List.Free;
end.
Run Code Online (Sandbox Code Playgroud) 我们有一些旧类使用Contnrs.TObjectList
,在某些情况下,自定义比较函数用于对这些列表进行排序,使用如下所示:
procedure TMyClass.SortItems;
function CompareFunction(Item1, Item2: Pointer): Integer;
begin
Result := TSomeItem(Item1).Value - TSomeItem(Item2).Value;
end;
begin
Sort(@CompareFunction);
end;
Run Code Online (Sandbox Code Playgroud)
在为Win32编译时,此代码没有问题,但是当我们为Win64编译它时,我们发现排序不再起作用.
我已经修改了一些,Generics.Collections.TObjectList<T>
而是修改了CompareFunction
它以及如何调用它.所以我的猜测是它与CompareFunction
被调用的方式有关,方法是在@
运算符前加上运算符,据我所知,它指的是函数的地址.
为什么上面的代码不适用于Win64,以及解决它的正确方法是什么?
为了解释我的问题,我将使用与Embarcadero网站上的TDictionary
示例非常相似的内容,但我的课程实现了IComparable
.
在Delphi(XE7)中:
TCity = class(TInterfacedObject, IComparable)
Country: String;
Latitude: Double;
Longitude: Double;
function CompareTo(Obj: TObject): Integer;
end;
Run Code Online (Sandbox Code Playgroud)
我希望TDictionary
以城市为关键:
Dictionary := TDictionary<TCity, string>.Create;
CityA := TCity.Create;
CityA.Country := 'United Kingdom';
CityA.Latitude := 51.5;
CityA.Longitude := -0.17;
Dictionary.Add(CityA, 'London');
Run Code Online (Sandbox Code Playgroud)
现在,如果我创建一个与之前CityA.CompareTo(CityB)
返回值相同的新城市0
,例如:
CityB := TCity.Create;
CityB.Country := 'United Kingdom';
CityB.Latitude := 51.5;
CityB.Longitude := -0.17;
Run Code Online (Sandbox Code Playgroud)
在添加CityB
到字典之前,我期待:
Dictionary.ContainsKey(CityB)
Run Code Online (Sandbox Code Playgroud)
会使用我的CompareTo
实现并ContainsKey
返回True
,但事实并非如此.看来我的班级也需要继承TEqualityComparer
.
那么如何在ContainsKey
我的课程中添加一个等级比较器,除了CompareTo
已经存在的等级比较器? …
在OS X应用程序中,如果我有一个带有几个按钮的表单,如何为表单指定默认按钮?通过默认按钮我的意思是行为,当我按下按钮进入.
我一直在努力学习Swift,所以我一直在尝试使用Xcode 6构建小型OS X应用程序.
我的所有编程经验都在Windows桌面应用程序中,所以我可能会看到这一切都错了,也许在这里使用错误的术语.