![]() |
Callback
Hallo
Mein Problem ist, dass ich probiere, Callback-funktionen in mein Programm ein zu bauen. Dies mach ich so:
Delphi-Quellcode:
Dieses Codestück rufe ich folgendermassen auf:
TForEachBillpos = procedure(billpos: IInicBillpos) of object;
TForEachBillposProduct = procedure(billposProduct: IInicBillposProduct) of object; procedure forEachBillpos(billing: IInicBilling; proc: TForEachBillpos); procedure forEachBillposProduct(billing: IInicBilling; proc: TForEachBillposProduct); overload; procedure forEachBillposProduct(billpos: IInicBillpos; proc: TForEachBillposProduct); overload; function _getForEachBillposMethod(self: TObject; proc: Pointer): TForEachBillpos; function _getForEachBillposProductMethod(self: TObject; proc: Pointer): TForEachBillposProduct; [...] procedure forEachBillpos(billing: IInicBilling; proc: TForEachBillpos); var it: IJclIntfIterator; begin it := billing.Billposes.Values.First; while it.HasNext do proc(it.Next as IInicBillpos); // <----- OK end; procedure forEachBillposProduct(billing: IInicBilling; proc: TForEachBillposProduct); procedure forEach(Self: pointer; billpos: IInicBillpos); begin forEachBillposProduct(billpos, proc); end; begin forEachBillpos(billing, _getForEachBillposMethod(nil, @forEach)); end; procedure forEachBillposProduct(billpos: IInicBillpos; proc: TForEachBillposProduct); var it: IJclIntfIterator; begin it := billpos.BillposProducts.Values.First; while it.HasNext do proc(it.Next as IInicBillposProduct); // <----- ACCESS VIOLATION end; function _getForEachBillposMethod(self: TObject; proc: Pointer): TForEachBillpos; begin TMethod(Result).Code := proc; TMethod(Result).Data := self; end; function _getForEachBillposProductMethod(self: TObject; proc: Pointer): TForEachBillposProduct; begin TMethod(Result).Code := proc; TMethod(Result).Data := self; end;
Delphi-Quellcode:
Mein Problem ist nun, dass der bei der markierten Stelle eine access violation auftritt, wobei die mit "OK" kommentierte Stelle einwandfrei funktioniert.
function hasBillingProduct(billing: IInicBilling; productId: integer): boolean;
procedure forEach(Self: pointer; billposProduct: IInicBillposProduct); begin Result := Result or (billposProduct.Product.Pid = productId); end; begin forEachBillposProduct(billing, _getForEachBillposProductMethod(nil, @forEach)); end; Wisst ihr, an was dies liegen könnte, respk. wie man das Callback-prinzip sonst wie lösen könnte? Besten Dank im Voraus |
Re: Callback
Es gibt Callback-Funktionen und Callback-Methoden.
In deinem Fall handelt es sich um Methoden, da du mit der Klausel "of object;" deklariert hast. Wenn ich mir folgendes Codestück anschaue:
Delphi-Quellcode:
dann fallen 2 Dinge auf:
function hasBillingProduct(billing: IInicBilling; productId: integer): boolean;
procedure forEach(Self: pointer; billposProduct: IInicBillposProduct); begin Result := Result or (billposProduct.Product.Pid = productId); end; begin forEachBillposProduct(billing, _getForEachBillposProductMethod(nil, @forEach)); end; * der self Zeiger ist auf nil gesetzt - das sollte bei Methodenzeigern nicht sein (du greifst zwar später nicht auf self zu, aber es macht schon ein ungutes Gefühl) * procedure forEach ist eine geschachtelte Prozedur. Diese darf eigentlich nur im Kontext der umliegenden funktion/prozedur hasBillingProduct ausgeführt werden. Durch deine Tricks hat die procedure forEach ein ganz anderes Stackumfeld, was wohl Probleme verursachen kann. |
Re: Callback
Ich habe die ganze Sache mal in eine Klasse gepackt und habe nun als self die instanz der Klasse übergeben. Das Ergebnis ist jedoch das selbe.
Gibt es demnach keine "einfache" Lösung, bei der nicht bei jedem Aufruf ein riesiger Overhead an Definitionen und Zusatzmethoden geschrieben werden muss? |
Alle Zeitangaben in WEZ +1. Es ist jetzt 19:21 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz