Recent

Author Topic: get Access Violation when close form using menu after form2 second open  (Read 9127 times)

irw10062

  • New Member
  • *
  • Posts: 31
need help, whats wrong with my code :

for test,
i use menuitem1 to create form2 and open it as showmodal,
after close form2 using menu tutup in form2 , in form1 i click menuitem1 again to show again form2 and close form2 using menututup in form2

after click menututup i get access violation...

i dont know what caused error AV ...

i put caFree in form2close not work too,

procedure TForm2.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  CloseAction := caFree;
end;

i see lcldebug.log contain

TApplication.HandleException Access violation
  Stack trace:
  $0001B730
  $F000FFFC
  $000230D4


Quote
procedure TForm1.MenuItem1Click(Sender: TObject);
begin

  //if Assigned(Form2) then
  //   ShowMessage('something wrong, form2 must be nil');

  Form2 := TForm2.Create(Self);
  try
    Form2.ShowModal;
  finally
    Form2.Free;
    //FreeAndNil(form2)
  end;
end;
     

i test in
- Lazarus-1.1-38850-fpc-2.6.0-20120927-win32.exe + Lazarus-1.1-38850-fpc-2.6.0-20120927-cross-arm-wince-win32.exe
- Lazarus-1.1-37629-fpc-2.6.1-20120612-cross-arm-wince-win32.exe + Lazarus-1.1-37629-fpc-2.6.1-20120612-win32.exe
- fpc 2.71 codetyphon too

thanks

Irwan

Dibo

  • Hero Member
  • *****
  • Posts: 1045
Re: get Access Violation when close form using menu after form2 second open
« Reply #1 on: October 03, 2012, 09:21:21 am »
I used KOL-CE long time ago, so I can't check solution, but what I remember is that you can't refer to TForm, but must  refer to TKolForm component which should be placed on the form (this component has also OnClose event). This hint is mentioned on http://wiki.freepascal.org/KOL-CE (wiki is down right now :/ ) .
You should checkout official kol demos http://kolmck.net/e_demos.htm , especialy "2 forms" (http://kolmck.net/demos/Demo2Forms.zip) and "modal forms" (http://kolmck.net/demos/DemoModalForm.zip) . Those demos probably will not work on Lazarus KOL-CE port, but you can look at  source code.
BTW: Do you solved problem with events error?

irw10062

  • New Member
  • *
  • Posts: 31
Re: get Access Violation when close form using menu after form2 second open
« Reply #2 on: October 03, 2012, 09:44:23 am »
sir,
for this thread is not kolce but using lcl standard component

vrull

  • Full Member
  • ***
  • Posts: 111
Re: get Access Violation when close form using menu after form2 second open
« Reply #3 on: October 03, 2012, 10:19:19 am »
Code: [Select]
Form2 := TForm2.Create(Self);
  try
    Form2.ShowModal;
  finally
    Form2 := nil;
    //Form2.Free;
    //FreeAndNil(form2)
  end;
As you set "CloseAction := caFree" the form will free itself. You only need to set its variable to nil, to re-use it next time. And your top line I would change as "if not Assigned(Form2) then ...create it"

irw10062

  • New Member
  • *
  • Posts: 31
Re: get Access Violation when close form using menu after form2 second open
« Reply #4 on: October 03, 2012, 02:00:59 pm »
Code: [Select]
Form2 := TForm2.Create(Self);
  try
    Form2.ShowModal;
  finally
    Form2 := nil;
    //Form2.Free;
    //FreeAndNil(form2)
  end;
As you set "CloseAction := caFree" the form will free itself. You only need to set its variable to nil, to re-use it next time. And your top line I would change as "if not Assigned(Form2) then ...create it"

no, my code above (see attach sample) not using "CloseAction := caFree"
i try your code, not work too, i think there is problem in menu, because when i put button with code "Close", the form2 close without AV
here compile exe with error AV
http://www.irwanto.net/free/tesmenu.7z



bambamns

  • Full Member
  • ***
  • Posts: 223
Re: get Access Violation when close form using menu after form2 second open
« Reply #5 on: October 05, 2012, 05:57:51 am »
HI,

First I hope you tranfered form2 from Auto-create forms to Available forms in Project->Project Options->Forms

If you did, try this :

Code: [Select]
Form2 := TForm2.Create(nil);
Form2.ShowModal;
Form2.Release;

On form2 just use close; to close form.

I use this in all WinCE and Windows 32/64 for modals, for years in Delphi and Lazarus and it works ok.

Lazarus 1.8.4 + FPC 2.6.4 x86 (rebuild) and Lazarus 2.0, Windows 7 x64, unless otherwise specified

irw10062

  • New Member
  • *
  • Posts: 31
Re: get Access Violation when close form using menu after form2 second open
« Reply #6 on: October 05, 2012, 09:12:53 am »
are you trying this sample?

i thing a problem is not on form2.free or form2.release or form2 := nil etc,,
but problem when click menu on form2 for close

Yogi

  • New Member
  • *
  • Posts: 42
Re: get Access Violation when close form using menu after form2 second open
« Reply #7 on: October 05, 2012, 10:23:30 am »
Hi

What exactly are You doing in the OnClick-Event of form2 ?
Calling simply "close" will fail like in Your case!
Closing a modal form should be done by assigning a value<>0 to ModalResult of the form. So in the OnClick-Event of the MenuItem You should do this:

ModalResult:=mrCancel // or anything other<>0

I think then all should work in Your case

Yogi
Lazarus 1.3 SVN 44197 * FPC 2.7.1 SVN 26822
Linux: 3.2.0-4-686-pae * Debian wheezy
Windows: W98