#1 2012-11-06 09:02:23

h.hasenack
Member
From: Nijmegen, Netherlands
Registered: 2012-08-01
Posts: 173
Website

TInterfacedObjectWithCustomCreate implementation error

I am pretty sure casting an unitialized result to an object will fail. I even tested it wink

function TServiceFactoryServer.CreateInstance: TInterfacedObject;
begin
  if fImplementationClassWithCustomCreate then
    result := TInterfacedObjectWithCustomCreate(result).Create else
    result := fImplementationClass.Create;
end;

What you probably mean is this:

type
  TInterfacedObjectWithCustomCreateClass=class of TInterfacedObjectWithCustomCreate;
...

function TServiceFactoryServer.CreateInstance: TInterfacedObject;
begin
  if fImplementationClass.InheritsFrom(TInterfacedObjectWithCustomCreate) then
    result := TInterfacedObjectWithCustomCreateClass(fImplementationClass).Create 
  else
    result := fImplementationClass.Create;
end;

My guess is you wont need the fImplementationClassWithCustomCreate private field either.

Last edited by h.hasenack (2012-11-06 09:05:24)

Offline

#2 2012-11-06 09:21:36

h.hasenack
Member
From: Nijmegen, Netherlands
Registered: 2012-08-01
Posts: 173
Website

Re: TInterfacedObjectWithCustomCreate implementation error

COme to think of it...

TServiceFactoryServer.CreateInstance

could very well return a TObject, there is no reason I can think of to make it return a TInterfacedObject... do you?

Offline

#3 2012-11-06 09:32:07

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: TInterfacedObjectWithCustomCreate implementation error

1. fImplementationClassWithCustomCreate is here to speed up the process.
Calling InheritsFrom() is slower.

2. We expect it to be an TInterfaceObject to be able to access the reference count directly.
We could use any TObject, and then call the IInterface members, but I did not want it, since some users may be confused and declare their implementation class as TObject, whereas we need them to implement interfaces.
So it was designed to avoid runtime errors.

Offline

#4 2012-11-06 09:48:19

h.hasenack
Member
From: Nijmegen, Netherlands
Registered: 2012-08-01
Posts: 173
Website

Re: TInterfacedObjectWithCustomCreate implementation error

1) Is good reasoning, I agree to that.
2) I mean the return value of type of TServiceFactoryServer.CreateInstance to be TObject rather than TInterfacedObject. The runtime error is avoided by the constructor insisting on a TInterfacedClass anyway.

Dont get me wrong, point 2 is very minor, now I feel like I'm nagging you wink. I also see now the point of reference counting you do with FSharedinstance. AMOF : just keep it as it is.

Except of course for the error mentioned earlier tongue

Last edited by h.hasenack (2012-11-06 10:06:05)

Offline

#5 2012-11-06 10:12:33

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: TInterfacedObjectWithCustomCreate implementation error

I've fixed the issue thanks to your feedback.

See http://synopse.info/fossil/info/646d312153

I should have taken better notice of the compiler warning!
smile

Offline

Board footer

Powered by FluxBB