AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Callback

Ein Thema von Sämy · begonnen am 15. Jan 2008 · letzter Beitrag vom 16. Jan 2008
Antwort Antwort
Sämy

Registriert seit: 4. Nov 2004
Ort: Basel (CH)
76 Beiträge
 
Delphi 2007 Professional
 
#1

Callback

  Alt 15. Jan 2008, 15:49
Hallo

Mein Problem ist, dass ich probiere, Callback-funktionen in mein Programm ein zu bauen. Dies mach ich so:

Delphi-Quellcode:
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;
Dieses Codestück rufe ich folgendermassen auf:

Delphi-Quellcode:
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;
Mein Problem ist nun, dass der bei der markierten Stelle eine access violation auftritt, wobei die mit "OK" kommentierte Stelle einwandfrei funktioniert.
Wisst ihr, an was dies liegen könnte, respk. wie man das Callback-prinzip sonst wie lösen könnte?

Besten Dank im Voraus
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
  Mit Zitat antworten Zitat
shmia

Registriert seit: 2. Mär 2004
5.508 Beiträge
 
Delphi 5 Professional
 
#2

Re: Callback

  Alt 15. Jan 2008, 16:55
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:
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;
dann fallen 2 Dinge auf:
* 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.
Andreas
  Mit Zitat antworten Zitat
Sämy

Registriert seit: 4. Nov 2004
Ort: Basel (CH)
76 Beiträge
 
Delphi 2007 Professional
 
#3

Re: Callback

  Alt 16. Jan 2008, 07:55
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?
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 21:48 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz