开发者

Access Violation when touching dynamic form?

I am creating a form dynamically, however the routine that closes it, is located in my Main Form's unit, because it's associated with a control on my Main Form (TSkype). The event is SkypeAsyncSearchUsersFinished. I try to set the ModalResult to mrOk of my dynamic form, when the SkypeAsyncSearchUsersFinished routine finishes. However, this produces an Access Violation.

This is the code that fires when the search has finished:

   if SIDList.Count = 0 then
   begin
     frmSearcher.tmrFadeOut.Enabled := 开发者_如何学GoTrue;
   end;

I tried debugging it, and I set a breakpoint at frmSearcher.tmrFadeOut... and I step to the next line (routine End;), and thats when the AV arises.

The tmrFadeOutTimer event does the ModalResult := mrOk;

This is how I create my dynamic form:

Function ShowSearcher():Boolean;
Var
   dlg : TfrmSearcher;
Begin
  Result := False;
  dlg := TfrmSearcher.Create(Forms.Application);


  dlg.tmrFadeIn.Enabled := True;


    if dlg.ShowModal = mrOk then
         Begin
         // Do nothing here

         End;

  Result := True;
  dlg.Release;

End;

Yes, I am sure that the timer only gets enabled once. :)

Yes, I am sure the form is "alive". :)

Actually, if I remove the tmrFadeOut.Enabled code, everything works fine, but the form does not get closed. Doing frmSearcher.ModalResult := mrOk; also produces an AV.

If you need more info, I will add it. :)

Thanks a bunch! :)


Since you're manually creating the TfrmSearcher form, remove the IDE-generated variable frmSearcher, fix the compiling errors you'll get and you'll be fine.

You get the AV because frmSearcher is NIL.


You are assigning your new TfrmSearcher object instance to a local dlg variable that no other method has access to. Your other routines are trying to access the dialog using a frmSearcher variable that you are not assigning any value to.


I'm adding this as an answer because I want to format code. It's not an attempt to answer the question and I'll likely delete in due course, especially if I get lots of down-votes (peer pressure).

What I wanted to show is how to create, show and destroy a form. You do it like this:

procedure ShowMyForm;
var
  Form: TMyForm;
begin
  Form := TMyForm.Create(nil);
  try
    Form.ShowModal;
  finally
    Form.Free;
  end;
end;

In your code:

  1. There is no need to pass an owner to the constructor since you are determining the life of the form.
  2. The return variable of the function is pointless since you always return True. The only failure mode is via an exception but then the function has no return value.
  3. Don't call Release where you did, plain old Free is what you want. You call Release when handling a message and you want the form to go away once any messages currently in the queue are dealt with.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜