Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#2

AW: Supports(..) liefert Referenz welche AV auslöst

  Alt 19. Jan 2015, 19:37
Nur so fürs Protokoll, versuche es mal so
Delphi-Quellcode:
program dp_183554;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils;

type
  IMyInterface = interface
    ['{88DDD9A5-F4BC-47B9-9240-31B1C986F230}']
    procedure doSomething( );
  end;

  TMyClass = class( TInterfacedObject, IMyInterface )
  public
    constructor Create( );
    procedure doSomething( );
  end;

procedure justSupportsThings( );
var
  myObject: TObject;
  myIntf: IMyInterface;
  castSuccessful: Boolean;
begin
  myObject := TMyClass.Create( );
  castSuccessful := Supports( myObject, IMyInterface, myIntf );
  if castSuccessful
  then
    myIntf.doSomething( );
end;

constructor TMyClass.Create( );
begin
  inherited;
  // Schwachfug
  // _AddRef(); // Damit mich das Supports(..) nicht abräumt
end;

procedure TMyClass.doSomething;
begin
  // nop
end;

begin
  try
    justSupportsThings( );
  except
    on E: Exception do
      Writeln( E.ClassName, ': ', E.Message );
  end;
  readln;

end.
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat