Recent

Author Topic: SIGSEGV when call-back to originating form.  (Read 1139 times)

OldRuby

  • Newbie
  • Posts: 3
SIGSEGV when call-back to originating form.
« on: August 13, 2020, 04:59:06 pm »
After invoking a second form B from form A, trying to call a procedure in form A from form B encounters a run-time error.  The attached minimum working example has the details.

Is this a bug, or something wrong with my coding?

Windows10 64-bit; Lazarus 2.0.8 32-bit.

Handoko

  • Hero Member
  • *****
  • Posts: 3752
  • My goal: build my own game engine using Lazarus
Re: SIGSEGV when call-back to originating form.
« Reply #1 on: August 13, 2020, 05:37:28 pm »
Hello OldRuby,
Welcome to the forum.

I downloaded and tested your code on my Lazarus 2.0.10 Linux. Unfortunately I got a compile-time error. Can you test it reattach the example again?

howardpc

  • Hero Member
  • *****
  • Posts: 3517
Re: SIGSEGV when call-back to originating form.
« Reply #2 on: August 13, 2020, 06:30:17 pm »
@OldRuby: try the amended example attached.

wp

  • Hero Member
  • *****
  • Posts: 7519
Re: SIGSEGV when call-back to originating form.
« Reply #3 on: August 13, 2020, 06:45:21 pm »
Is this a bug, or something wrong with my coding?
In TFormB.FormActivate you access AForm which does not exist. The TFormA instance you created was frmA which, moreover, is local and cannot be accessed any more after leaving TMainForm.Button1Click
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

OldRuby

  • Newbie
  • Posts: 3
Re: SIGSEGV when call-back to originating form.
« Reply #4 on: August 13, 2020, 07:17:08 pm »
Thanks for the replies.

Hello OldRuby,
Welcome to the forum.

I downloaded and tested your code on my Lazarus 2.0.10 Linux. Unfortunately I got a compile-time error. Can you test it reattach the example again?

If you comment out the whole for-statement, then it should compile on Linux.

@OldRuby: try the amended example attached.

If I replace the writeln statment with a MessageDlg, then your example works, thank you.

Is this a bug, or something wrong with my coding?
In TFormB.FormActivate you access AForm which does not exist. The TFormA instance you created was frmA which, moreover, is local and cannot be accessed any more after leaving TMainForm.Button1Click

I'll ponder this and work out how to use the information.  I'm also learning how to post and reply on this forum!

howardpc

  • Hero Member
  • *****
  • Posts: 3517
Re: SIGSEGV when call-back to originating form.
« Reply #5 on: August 14, 2020, 09:39:03 am »
I didn't look at your project but I am going to take a guess..

... BUt like I said, without actually looking at it its hard to say..
Jamie, without actually looking it is impossible to say with certainty.
wp already looked, and provided a clear, concise explanation of the code's faults.

So it is pointless for you to guess and  reply saying nothing worth adding.

OldRuby

  • Newbie
  • Posts: 3
SOLVED Re: SIGSEGV when call-back to originating form.
« Reply #6 on: August 16, 2020, 03:51:46 pm »
Is this a bug, or something wrong with my coding?
In TFormB.FormActivate you access AForm which does not exist. The TFormA instance you created was frmA which, moreover, is local and cannot be accessed any more after leaving TMainForm.Button1Click
The issue was the way I was trying to call back to Form A from Form B, using an inaccessible variable.  Given that, it was easy to get a valid pointer to Form A from the constructor of Form B.  Here's the revised part of my example:
Code: Pascal  [Select][+][-]
  1. constructor TFormb.Create(Sender : TComponent);
  2. begin
  3.   inherited Create(Sender);
  4.   AForm := Sender
  5. end;
  6.  
  7. procedure TFormb.FormActivate(Sender: TObject);
  8. begin
  9.     (*
  10.     ** Set up own elements, then call back to formA to set up
  11.     ** more, using DoSomething()
  12.     *)
  13.     TFormA(AForm).GetData(Self)
  14. end;
  15.  

That solution is now implemented in my actual application. Thanks for all the help.

wp

  • Hero Member
  • *****
  • Posts: 7519
Re: SIGSEGV when call-back to originating form.
« Reply #7 on: August 16, 2020, 04:21:49 pm »
You at least must check in FormActivate whether AForm really is a TFormA instance because the parameter passed to the constructor Create of a class is on a TComponent, i.e. can almost be anything! Usually it is Application, sometimes even nil.

Code: Pascal  [Select][+][-]
  1. procedure TFormb.FormActivate(Sender: TObject);
  2. begin
  3.     (*
  4.     ** Set up own elements, then call back to formA to set up
  5.     ** more, using DoSomething()
  6.     *)
  7.     if AForm is TFormA then
  8.       TFormA(AForm).GetData(Self)
  9.     else
  10.       raise Exception.Create('No usable callback form available.');
  11. end;

The exception reminds you that your code is doing something very wrong.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

 

TinyPortal © 2005-2018