讓我們假設我有以下聲明如下自定義列表:當我重新排列TObjectList時,爲什麼會出現「無效指針操作」?
type
TCustomList = class(TObjectList)
private
function GetItem(AIndex: Integer): TMyObject; // virtual;
procedure SetItem(Index: Integer; AObject: TMyObject);
...
public
property Items[Index: Integer]: TMyObject read GetItem write SetItem;
procedure InsertSort();
end;
通過以下實現:
implementation
function TCustomList.GetItem(AIndex: Integer): TMyObject;
begin
Result := TMyObject(inherited Items[AIndex]);
end;
procedure TCustomList.SetItem(Index: Integer; AObject: TMyObject);
begin
inherited Items[Index]:= AObject;
end;
procedure TCustomList.InsertSort;
var
i, j: integer;
key: TMyObject;
begin
for j := 1 to self.Count - 1 do
begin
key:= self.Items[j];
i := j - 1;
while ((i >= 0) AND (self.Items[i].Compare(key)>0)) do
begin
self.Items[i+1]:= self.Items[i]; // does not WORK!!! properly.. System.Contnrs problem ??
i := i-1;
end; // while
self.Items[i+1]:= key;
end; // for
end; // procedure InsertSort
正如我以上的TMyObject
實例的集合運行的代碼,我得到一個無效的指針操作異常。我相信這是由於的元素通過Items
屬性讀取和寫入不佳而造成的。
爲什麼這個無效指針操作異常出現?
謝謝!我也認爲TObjectList正是這樣做的,即試圖摧毀同一個對象兩次。我會嘗試你的解決方案,併發布,如果它的工作。 – kenny 2013-03-05 15:44:34
一個解決方案,如你所建議的:使用內置的TList.Sort函數,如下所示: 調用'myCustomList.Sort(@ MyObject.Compare)' - 帶有@ MyObject.Compare是一個指向TMyObject內部實現的比較函數的指針。 我試過的第二種方法是: invoke 'self.OwnsObjects:= false;'在TCustomList.InsertSort的for循環之前的 ;然後'self.OwnsObjects:= true;'排序完成後,排序爲 。 謝謝,大衛,我會掙扎很多。 – kenny 2013-03-05 16:05:13
您需要將比較函數實現爲獨立函數而不是實例方法。你的代碼調用'Sort'可能有效,但這有點意外。一個建議的話。不要使用@來獲取函數指針。 – 2013-03-05 16:12:48