Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Why Assigned Returns True ?


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 9 - Last Post: Jan 5, 2018 11:52 AM Last Post By: Rudy Velthuis (... Threads: [ Previous | Next ]
wesley bobato

Posts: 19
Registered: 3/17/10
Why Assigned Returns True ?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 4, 2018 7:57 AM
Hello guys.

can anyone please explain to me why the assigned return true?

unit Unit1;

interface

uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
FMX.Controls.Presentation, FMX.StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
FButton: TButton;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.Button1Click(Sender: TObject);
var
btn: TButton;
begin
btn := TButton.Create( nil );
FButton := @btn;
FreeAndNil( btn );
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if Assigned( FButton ) then
ShowMessage( 'yes' );
end;

end.

thank you
Jeff Overcash (...

Posts: 1,529
Registered: 9/23/99
Re: Why Assigned Returns True ?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 4, 2018 9:56 AM   in response to: wesley bobato in response to: wesley bobato
On 1/4/2018 10:57 AM, wesley bobato wrote:
Hello guys.

can anyone please explain to me why the assigned return true?

unit Unit1;

interface

uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
FMX.Controls.Presentation, FMX.StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
FButton: TButton;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.Button1Click(Sender: TObject);
var
btn: TButton;
begin
btn := TButton.Create( nil );
FButton := @btn;
FreeAndNil( btn );
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if Assigned( FButton ) then
ShowMessage( 'yes' );
end;

end.

thank you

FreeAndNil only nil's the variable passed to it, not all variables pointing to
that address location. So after FreeAndNil btn is nil but FButton is not.
FButton points to an invalid memory location (non ARC).

--
Jeff Overcash (TeamB)
(Please do not email me directly unless asked. Thank You)
Learning is finding out what you already know. Doing is demonstrating that you
know it. Teaching is reminding others that they know it as well as you. We are
all leaners, doers, teachers. (R Bach)
wesley bobato

Posts: 19
Registered: 3/17/10
Re: Why Assigned Returns True ?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 4, 2018 10:08 AM   in response to: Jeff Overcash (... in response to: Jeff Overcash (...
Hi @Jeff Overcash

Thank you very much, very enlightening.
How do I know if this memory location is valid or not?

Assigned returns a false positive.

Kind regards.
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: Why Assigned Returns True ?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 4, 2018 10:53 AM   in response to: wesley bobato in response to: wesley bobato
wesley bobato wrote:

Hi @Jeff Overcash

Thank you very much, very enlightening.
How do I know if this memory location is valid or not?

Assigned returns a false positive.

Kind regards.

You don't. You should design your software thus that you don't have to
guess or check, in other words, you should design it thus that you
know, at all times, which objects are valid and which aren't. That is
not always a trivial matter

How this can be achieved can for instance be found in Dalija
Prasnikar's book: "Delphi Memory Management for classic and ARC
compilers".
https://www.artplussoftware.com/dalijap/delphimm/index.html

--
Rudy Velthuis http://www.rvelthuis.de

"An effective way to deal with predators is to taste terrible."
wesley bobato

Posts: 19
Registered: 3/17/10
Re: Why Assigned Returns True ?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 4, 2018 11:23 AM   in response to: Rudy Velthuis (... in response to: Rudy Velthuis (...
Our incredible this book from @Dalija
Surely I will buy and print it :)

Thank you for your words.
Lajos Juhasz

Posts: 801
Registered: 3/14/14
Re: Why Assigned Returns True ?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 4, 2018 10:59 AM   in response to: wesley bobato in response to: wesley bobato
wesley bobato wrote:

Hi @Jeff Overcash

Thank you very much, very enlightening.
How do I know if this memory location is valid or not?

Assigned returns a false positive.

Kind regards.

It's not a falser positive. The variable is not nil thus Assigned
returns true.

In a non ARC you've to know your job and make sure that a reference
points to a valid object.
wesley bobato

Posts: 19
Registered: 3/17/10
Re: Why Assigned Returns True ?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 4, 2018 11:20 AM   in response to: Lajos Juhasz in response to: Lajos Juhasz
Thanks for your help.
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: Why Assigned Returns True ?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 5, 2018 11:52 AM   in response to: Lajos Juhasz in response to: Lajos Juhasz
Lajos Juhasz wrote:

wesley bobato wrote:

Hi @Jeff Overcash

Thank you very much, very enlightening.
How do I know if this memory location is valid or not?

Assigned returns a false positive.

Kind regards.

It's not a falser positive.

It is not really a false positive. Assigned works correctly. But from
the POV of the user, it is a "false positive", because the user
expected to detect invalid pointers this way.

--
Rudy Velthuis http://www.rvelthuis.de

"An inconvenience is only an adventure wrongly considered; an
adventure is an inconvenience rightly considered."
-- Gilbert Keith Chesterton (1874-1936)

wesley bobato

Posts: 19
Registered: 3/17/10
Re: Why Assigned Returns True ?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 4, 2018 10:23 AM   in response to: Jeff Overcash (... in response to: Jeff Overcash (...
Jeff Overcash

I removed the comment because it did not work

Kind regards.

Edited by: wesley bobato on Jan 4, 2018 10:28 AM

Edited by: wesley bobato on Jan 4, 2018 10:29 AM
wesley bobato

Posts: 19
Registered: 3/17/10
Re: Why Assigned Returns True ?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 4, 2018 10:37 AM   in response to: wesley bobato in response to: wesley bobato
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02