aardvarkk
aardvarkk

Reputation: 15996

Delphi TForm OnCreate gets called multiple times

I hope I can explain this problem decently well!

I'm trying to implement a ReWire audio device as a Delphi .dll. If you don't know what ReWire is, don't worry about it. What's important is that my code compiles into a .dll, and I get calls from the ReWire system into my .dll to open up a display, check if the display is opened, and close it again.

When I get the call to launch, I do the following:

  if not Assigned(form) then
    form := TMyForm.Create(nil);
  form.Show;

where form is a global variable inside of my Delphi library (maybe a problem?). I have hooked up MyForm's OnCreate event to do some interesting things like prepare an array of stuff I want to work with.

So far everything's good. My form has a little button in it that opens up a TOpenDialog. I find that as soon as that dialog closes, somehow the OnCreate event is firing again in my form!

I have checked that OnDestroy is not being called, so I have no idea why OnCreate is getting called again.

Unfortunately I'm not really sure what information is relevant, but here's the call stack the first time around (when the form is first set up):

First call stack

As expected, ReWire is making a call into my .dll to Launch the Panel application, so I create my form. Great, things are looking good.

Then inside my form, I open up a little dialog, select a file, and do some operations. Out of left field, OnCreate is called again, and here's the call stack that time:

Second call stack

It's a crazy party of calls! Reaper (at the bottom) is the ReWire host I'm using to test my application, but I have no idea what's going on inside that stack trace because none of it is my code. Suddenly the event just gets called when I don't think it should, because OnDestroy wasn't even called.

The only other important thing I can think of is that if I print out the address of the Sender, it's different each time, so it's somehow getting Created again or something, but I've checked that I only call the MyForm.Create once.

Any ideas as to how this type of thing could happen?

Upvotes: 2

Views: 900

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 595896

In the first stack trace, the OnCreate call is preceded by a call to TCustomForm.Create(), which is correct behavior. In the second stack trace, the OnCreate call is preceded by a call to TObject.Create() instead, which is not correct behavior. That leads me to think that something in your button OnClick event handler is either constructing an object with a bad VMT, or is otherwise corrupting memory in general and causing a bad jump into code that just happens to be occupied by your TForm class. Either way, double check your OnClick logic for bugs.

Upvotes: 4

Rob McDonell
Rob McDonell

Reputation: 1319

Check on (any)where you are setting the form variable to nil. It may be that it's being set to nil without freeing the form it's pointing to, and so next time your launch code is called it's creating another instance of the form.

Upvotes: 1

Related Questions