Recent

Author Topic: [SOLVED] Self-creating objects?  (Read 2493 times)

miki

  • New member
  • *
  • Posts: 7
[SOLVED] Self-creating objects?
« on: November 08, 2016, 08:26:41 pm »
Hello.

Maybe I have a misconception with the language (freepascal), but I saw something I think is strange.

Have this example class:
Code: Pascal  [Select]
  1.     TMyObject = class
  2.     private
  3.         fstr: String;
  4.     public
  5.         constructor Create(param: String);
  6.         function getStr: String;
  7.     end;
  8.  
  9. constructor TMyObject.Create(param: String);
  10. begin
  11.     fstr := param;
  12. end;
  13.  
  14. function TMyObject.getStr: String;
  15. begin
  16.     Result := fstr;
  17. end;
  18.  

And this code
Code: Pascal  [Select]
  1. procedure TfrmMain.Button1Click(Sender: TObject);
  2. var
  3.     obj: TMyObject;
  4. begin
  5.     ShowMessage(obj.getStr);
  6. end;
  7.  

I expect to have some problems on the ShowMessage line, runtime error or alike. But no, it works, showing weird characters in the screen.

Shouldn't I instantiate obj first, with method TMyObject.Create(String), before calling its methods?

Also, if I check obj agains nil, it fails (says it's not nil), unless I initialize the variable, like this:
Code: Pascal  [Select]
  1. procedure TfrmMain.Button1Click(Sender: TObject);
  2. var
  3.     obj: TMyObject;
  4. begin
  5.     if obj = nil then ShowMessage('obj is nil'); //Nothing shows
  6. end;
  7.  
  8. procedure TfrmMain.Button1Click(Sender: TObject);
  9. var
  10.     obj: TMyObject = nil;
  11. begin
  12.     if obj = nil then ShowMessage('obj is nil'); //now I get the message
  13. end;
  14.  

It looks like obj is created automatically, but I don't know why.

Any help or explanations?

Thanks!
« Last Edit: November 08, 2016, 09:00:05 pm by miki »

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 675
    • Lebeau Software
Re: Self-creating objects?
« Reply #1 on: November 08, 2016, 08:45:31 pm »
Maybe I have a misconception with the language (freepascal), but I saw something I think is strange.

Your conception is correct.  The example code is flawed.

I except to have some problems on the ShowMessage line, runtime error or alike. But no, it works, showing weird characters in the screen.

The code has undefined behavior.  The obj pointer is not initialized, so its fstr member will be invalid, so anything could happen when getStr() is called.  In your case, it is simply displaying garbage from random memory.  It could just as easily crash instead.

Shouldn't I instantiate obj first, with method TMyObject.Create(String), before calling its methods?

Yes.

Also, if I check obj agains nil, it fails (says it's not nil)

Right, because a local variable that is a non-managed type is not automatically initialized, it will pick up whatever random value was already present in its slot on the stack.  An object pointer is not a managed type, so a local object pointer is not automatically initialized to nil.

It looks like obj is created automatically, but I don't know why.

It is not.
« Last Edit: November 08, 2016, 08:47:19 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

miki

  • New member
  • *
  • Posts: 7
Re: Self-creating objects?
« Reply #2 on: November 08, 2016, 08:59:52 pm »
Thanks a lot for comments in every single sentence I wrote :)

I will have to take this into account: object references are not nil-default.

Regards.

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 670
Re: [SOLVED] Self-creating objects?
« Reply #3 on: November 08, 2016, 11:15:42 pm »
For debugging this kind of errors, the -CR command line option is very useful: it will check whether the self pointer is valid every time you call a method (and every time you typecast an instance pointer to a different class type).

BeniBela

  • Hero Member
  • *****
  • Posts: 684
    • homepage
Re: Self-creating objects?
« Reply #4 on: November 08, 2016, 11:56:02 pm »
Quote
Code: [Select]
procedure TfrmMain.Button1Click(Sender: TObject);
var
    obj: TMyObject = nil;
begin
    if obj = nil then ShowMessage('obj is nil'); //now I get the message
end;



Local variables with initializers.

Is that  a new feature?



Right, because a local variable that is a non-managed type is not automatically initialized, it will pick up whatever random value was already present in its slot on the stack.  An object pointer is not a managed type, so a local object pointer is not automatically initialized to nil.

Managed or not makes no difference. Local variables are not initialized to nil.

They are initialized to something, but it can just as well be a non-nil value.

Thaddy

  • Hero Member
  • *****
  • Posts: 9184
Re: [SOLVED] Self-creating objects?
« Reply #5 on: November 09, 2016, 03:44:04 am »
Local managed types are initialized. Managed types are always initialized. On create.
Unmanaged local types are "dirty" i.e. whatever is on the stack there..

Don't expect old school objects to be a managed type

But indeed, var obj = nil is valid code since at least 3.0
« Last Edit: November 09, 2016, 08:55:28 am by Thaddy »
also related to equus asinus.