Sign in Join

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

Mailing lists / fpc-pascal / 2018-03

Using Supports() I got an AV... bug?

9 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Marcos Douglas B. Santos

The code below should work but I get an AV when the program calls `Bar.Free`.

Do you know why?

Lazarus 1.8.1 r57299 FPC 3.0.4 i386-win32-win32/win64

===begin===
program Project1;
uses
  SysUtils;
type
  IBar = interface
  ['{C22FB8F4-1EC6-42C4-81E4-F2A52CC52258}']
    function Name: string;
  end;
  TBar = class(TInterfacedObject, IBar)
  public
    function Name: string;
  end;

{ TBar }

function TBar.Name: string;
begin
  Result := ClassName;
end;
var
  Bar: TBar;
begin
  Bar := TBar.Create;
  try
    if Supports(Bar, IBar) then
      Writeln('Bar supports...');
  finally
    Bar.Free;
  end;
end.
===end===

Regards,
Marcos Douglas

Reply by email

Vojtěch Čihák

Hi,
 
Bar shoud be released by Interface, no need to call Free.

V.
______________________________________________________________

Show quoted text 5 lines
Od: "Marcos Douglas B. Santos" Komu: FPC-Pascal users discussions Datum: 11.03.2018 14:23 Předmět: [fpc-pascal] Using Supports() I got an AV... bug?

The code below should work but I get an AV when the program calls `Bar.Free`.Do you know why?Lazarus 1.8.1 r57299 FPC 3.0.4 i386-win32-win32/win64...Regards,Marcos Douglas

----------

Reply by email

Vojtěch Čihák
to Vojtěch Čihák:   Bar shoud be released by Interface, no need to call Free. V.…

Yes, TBar is TInterfacedObject and is reference counted therefore Free is not needed. Just comment the line //Bar.Free; compile with -gh and you will see no memory leak.  
V.
______________________________________________________________

Show quoted text 5 lines
Od: "Marcos Douglas B. Santos" Komu: FPC-Pascal users discussions Datum: 11.03.2018 14:57 Předmět: Re: [fpc-pascal] Using Supports() I got an AV... bug?
Vojtěch Čihák wrote
Hi, Bar shoud be released by Interface, no need to call Free.

I'm sorry, but `Bar` was defined as `Bar: TBar` so, it's a class not an interface.

Marcos Douglas

Reply by email

Marcos Douglas B. Santos
to Vojtěch Čihák
Vojtěch Čihák wrote 5 lines
Yes, TBar is TInterfacedObject and is reference counted therefore Free is not needed. Just comment the line //Bar.Free; compile with -gh and you will see no memory leak.

You think this is the problem...
All right, try to change your copy like this:

===begin===
  Bar := TBar.Create;
  try
    if Supports(Bar, IBar) then
      Writeln(Bar.Name + ' supports...');
  finally
    ////Bar.Free;  << no Free
  end;
===end===

Are you still seeing an AV?

Marcos Douglas

Reply by email

Vojtěch Čihák

With this modification and -gh it is alright:
 
Interface]$ ./test
Bar supports...
Heap dump by heaptrc unit
7 memory blocks allocated : 875/880
7 memory blocks freed     : 875/880
0 unfreed memory blocks : 0
True heap size : 360448
True free heap : 360448
Interface]$
 
V.
______________________________________________________________

Show quoted text 5 lines
Od: "Marcos Douglas B. Santos" Komu: FPC-Pascal users discussions Datum: 11.03.2018 15:36 Předmět: Re: [fpc-pascal] Using Supports() I got an AV... bug?

===begin===
 Bar := TBar.Create;
 try
   if Supports(Bar, IBar) then
     Writeln(Bar.Name + ' supports...');
 finally
   ////Bar.Free;  << no Free
 end;
===end===

Are you still seeing an AV?

Marcos Douglas

Reply by email

Vojtěch Čihák
to Vojtěch Čihák: With this modification and -gh it is alright:   Interface]$ ./test Bar supports... Heap…

I only added {$mode objfpc} to be able compile, so I test this code:  
program Project1;
{$mode objfpc}
 
uses
  SysUtils;
 
type
  IBar = interface
  ['{C22FB8F4-1EC6-42C4-81E4-F2A52CC52258}']
    function Name: string;
  end;
 
  TBar = class(TInterfacedObject, IBar)
  public
    function Name: string;
  end;
 
{ TBar }
 
function TBar.Name: string;
begin
  Result := ClassName;
end;
 
var
  Bar: TBar;
 
begin
  Bar := TBar.Create;
  try
    if Supports(Bar, IBar) then
      Writeln('Bar supports...');
  finally
    //FreeAndNil(Bar);
  end;
 
end.           
 
My FPC is also 3.0.4, on 64-bit Linux.
Compiled with -gh I have no memory leak since this demo is about COM interfaces.  
V.
______________________________________________________________

Show quoted text 5 lines
Od: "Marcos Douglas B. Santos" Komu: FPC-Pascal users discussions Datum: 11.03.2018 16:17 Předmět: Re: [fpc-pascal] Using Supports() I got an AV... bug?
Vojtěch Čihák wrote
With this modification and -gh it is alright:

Only if you declare `Bar` as `IBar` instead of `TBar`. Please, check your code again.

Regards,
Marcos Douglas

Reply by email