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

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

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

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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

vojtech.cihak

Hi,

 

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


V.

______________________________________________________________
> Od: "Marcos Douglas B. Santos" <[hidden email]>
> Komu: FPC-Pascal users discussions <[hidden email]>
> 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


----------

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

Marcos Douglas B. Santos
On Sun, Mar 11, 2018 at 10:52 AM, Vojtěch Čihák <[hidden email]> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

vojtech.cihak
In reply to this post by vojtech.cihak

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.

______________________________________________________________
> Od: "Marcos Douglas B. Santos" <[hidden email]>
> Komu: FPC-Pascal users discussions <[hidden email]>
> Datum: 11.03.2018 14:57
> Předmět: Re: [fpc-pascal] Using Supports() I got an AV... bug?
>

On Sun, Mar 11, 2018 at 10:52 AM, Vojtěch Čihák <[hidden email]> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

Marcos Douglas B. Santos
On Sun, Mar 11, 2018 at 11:17 AM, Vojtěch Čihák <[hidden email]> wrote:
> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

vojtech.cihak
In reply to this post by Marcos Douglas B. Santos

With this modification and -gh it is alright:

 

[v1@nb-msi 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

[v1@nb-msi Interface]$

 

V.

______________________________________________________________
> Od: "Marcos Douglas B. Santos" <[hidden email]>
> Komu: FPC-Pascal users discussions <[hidden email]>
> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

Marcos Douglas B. Santos
Not here. I'm using FPC 3.0.4.
I've took a look at EMB documentation:
http://docwiki.embarcadero.com/Libraries/XE2/en/System.SysUtils.Supports
Take a look at "Warning" section. That's the problem.

Marcos Douglas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

Marcos Douglas B. Santos
In reply to this post by vojtech.cihak
On Sun, Mar 11, 2018 at 11:51 AM, Vojtěch Čihák <[hidden email]> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

vojtech.cihak
In reply to this post by vojtech.cihak

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.

______________________________________________________________
> Od: "Marcos Douglas B. Santos" <[hidden email]>
> Komu: FPC-Pascal users discussions <[hidden email]>
> Datum: 11.03.2018 16:17
> Předmět: Re: [fpc-pascal] Using Supports() I got an AV... bug?
>

On Sun, Mar 11, 2018 at 11:51 AM, Vojtěch Čihák <[hidden email]> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal