Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2007-08

Problem with interfaces

6 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Joao Morais

Hello,

I have the following code:

{$mode objfpc}
interface

...

function DefaultObj: TObject;
implementation
var
   _Holder: IHolder;
function DefaultObj: TObject;
begin
   if not Assigned(_Holder) then
     _Holder := THolder.Create(TSomeClass.Create);
   Result := _Holder.Instance;
end;

IHolder is an interface that the THolder class implements. The first call to the DefaultObj function creates the holder instance, as expected. New calls to the same function doesn't recreate it. Again, as expected.

The first call that I make to DefaultObj from *another* unit recreate the object, because, I don't know why, the _holder var points again to nil.

Changing the type of the _holder to THolder (ie an object pointer instead of an interface pointer) everything works flawlessly.

I have tried to create a small sample but couldn't reproduce the issue, anyway the source of the problem is gpl code. It fails under 2.0.4 and current fixes_2_2 branch. Hints?

Thanks.

Joao Morais
Darius Blaszijk
to Joao Morais

I've had similar issues some time ago. Have you tried FPC 2.3.1? It has a some interface fixes.

Darius

Joao Morais wrote 50 lines
Hello, I have the following code: {$mode objfpc} interface ... function DefaultObj: TObject; implementation var _Holder: IHolder; function DefaultObj: TObject; begin if not Assigned(_Holder) then _Holder := THolder.Create(TSomeClass.Create); Result := _Holder.Instance; end; IHolder is an interface that the THolder class implements. The first call to the DefaultObj function creates the holder instance, as expected. New calls to the same function doesn't recreate it. Again, as expected. The first call that I make to DefaultObj from *another* unit recreate the object, because, I don't know why, the _holder var points again to nil. Changing the type of the _holder to THolder (ie an object pointer instead of an interface pointer) everything works flawlessly. I have tried to create a small sample but couldn't reproduce the issue, anyway the source of the problem is gpl code. It fails under 2.0.4 and current fixes_2_2 branch. Hints? Thanks. -- Joao Morais _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Joao Morais
to Darius Blaszijk
Darius Blaszijk wrote
I've had similar issues some time ago. Have you tried FPC 2.3.1? It has a some interface fixes.

Nope. Trunk, rev. 8290 and still broken :-/

Thanks.

Joao Morais > > Joao Morais wrote: >> >> Hello, >> >> I have the following code: >> >> {$mode objfpc} >> interface >> >> ... >> >> function DefaultObj: TObject; >> >> implementation >> >> var >> _Holder: IHolder; >> >> function DefaultObj: TObject; >> begin >> if not Assigned(_Holder) then >> _Holder := THolder.Create(TSomeClass.Create); >> Result := _Holder.Instance; >> end; >> >> IHolder is an interface that the THolder class implements. The first >> call to the DefaultObj function creates the holder instance, as >> expected. New calls to the same function doesn't recreate it. Again, >> as expected. >> >> The first call that I make to DefaultObj from *another* unit recreate >> the object, because, I don't know why, the _holder var points again to >> nil. >> >> Changing the type of the _holder to THolder (ie an object pointer >> instead of an interface pointer) everything works flawlessly. >> >> I have tried to create a small sample but couldn't reproduce the >> issue, anyway the source of the problem is gpl code. It fails under >> 2.0.4 and current fixes_2_2 branch. Hints? >> >> Thanks. >> >> -- >> Joao Morais >> >
Micha Nelissen
to Joao Morais: I have the following code: {$mode objfpc} interface ... function DefaultObj: TObject;…
Joao Morais wrote
The first call that I make to DefaultObj from *another* unit recreate the object, because, I don't know why, the _holder var points again to nil.

Try to use a data watchpoint to watch when the variable is being changed. Hopefully that will point to the root cause. Did you also try printing the address of the variable (I assume global variables aren't moving, but who knows?)

Micha

Joao Morais
to Micha Nelissen
Micha Nelissen wrote 6 lines
Joao Morais wrote: > The first call that I make to DefaultObj from *another* unit recreate > the object, because, I don't know why, the _holder var points again to nil. Try to use a data watchpoint to watch when the variable is being changed. Hopefully that will point to the root cause.

Thanks for the advice.

Have tried 'watch _MVPFACTORY', gdb confirmed a hardware watchpoint, it complains a little (Hardware watchpoint 1: _MVPFACTORY -- 20 times) when I run the application but don't stop. Did I miss something?

Did you also try printing the address of the variable (I assume global variables aren't moving, but who knows?)

Always the same address.

Well, what I have observed so far: this issue happens when the variable points to an interface, changing to an object pointer the problem disapear. Moreover the _release method of the interface isn't called so it sounds rather impossible that my code is changing the value of the variable. Compiling and launching the code with D5 the problem disapear.

I simply don't know how to register the issue since I can't create a small sample that reproduce it, but I can send about 1 mb of sources to a volunteer though.

Thanks for your help.

Joao Morais
Joao Morais
to Joao Morais
Joao Morais wrote
I simply don't know how to register the issue since I can't create a small sample that reproduce it, but I can send about 1 mb of sources to a volunteer though.

Was this one forgotten? Let me know how can I help.

Joao Morais