Sign in Join

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

Mailing lists / fpc-devel / 2006-01

interface vs optimizer

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

mariano podesta

hi people,
there is a problem with functions returning interfaces: if nobody stores the result, the object decrements one reference despite turn off optimizations.

sample:

function getobj: IUnknown;
begin
  Result := TInterfacedObject.Create;
end;
procedure test;
begin
  getobj;
  // here result of getobj has died :(
end;

es there a solution or workaround?

thanks,
marianop

Reply by email

Alexey Barkovoy
to mariano podesta

This is by design.

Earlier message 33 lines
----- Original Message ----- From: mariano podesta To: Sent: Saturday, January 21, 2006 1:56 AM Subject: [fpc-devel] interface vs optimizer hi people, there is a problem with functions returning interfaces: if nobody stores the result, the object decrements one reference despite turn off optimizations. sample: function getobj: IUnknown; begin Result := TInterfacedObject.Create; end; procedure test; begin getobj; // here result of getobj has died :( end; es there a solution or workaround? thanks, marianop

Reply by email

Florian Klaempfl
to mariano podesta
mariano podesta wrote
ok, but is this a correct behavior?

Yes. Why should the interface life longer? There isn't any owner anymore? If you need a longer life time, you need to assign the interface to something.

i think it would be best to optimize with a compiler switch or to give a switch to turn it off. i have big porting problems :(

Porting from what? Delphi? Are you sure that it didn't on Delphi by accident?

Reply by email

mariano podesta
to Florian Klaempfl
Yes. Why should the interface life longer? There isn't any owner anymore? If you need a longer life time, you need to assign the interface to something.

i have many structures like this (yes, in delphi):

function TXList.Lock: ILock;
begin
  result := TLock.Create(self);
end;
procedure dostuff;
begin
  list.Lock;
  // use the locked list
  // when ILock is out of scope, unlocks the list
end;

i know the obvious solution is to assign it:

procedure dostuff;
var
  l: ILock;
begin
  l := list.Lock; // assign it
  // use the locked list
  // when ILock is out of scope, unlocks the list
end;

but it is a lot of work (it is a big project) and my pals are not so happy ):-\

Porting from what? Delphi? Are you sure that it didn't on Delphi by

accident?

i really don't know if it was accident, but we found it useful.

Reply by email