WindowObjeckt.ShowModal doesn't call the WindowObject.Destructor when scope is left

Hello,

I hope you can help me to understand the behaviour of MemoryManagement. I thought when the scope of a local object variable is left, the destructor of this object is called, before the object is destroyed.
The dlgUser destructor is called when the action ends. The Cancel and save buttons of the dialog only hide the window.

But if i don’t call dlgUser.Close at the end of the action, the constructor of dlgUser is not called, although the scope of the local dlgUser variable is left when the action ends. Does it mean, that the window is not destroyed an I have a memory leak, when I call the same Action again and again?

Sub Action
Dim dlgUser As New WinUser

dlgUser.ShowModal

// Nur in der DB Speichern, wenn der Save Button gedrückt wurde
If dlgUser.blnSaveButtonClicked = True Then
// Den Datensatz zur Datenbank hinzufügen
Self.Users.AddUser(dlgUser.txtName.Text, dlgUser.txtMedikament.Text)
End If

// destroys the dialog object. It is important to do it here because the window object
// is not destroyed automatically when the scope is lost by ending this function
dlgUser.Close
End Sub

Rarely if ever do you need to call a DESTRUCTOR…
XOJO manages that for you

And having a CLOSE right after is SHOWMODAL is useless. as the Window was closed when the SHOWMODAL completed (used with SHOW is different)

Per the Lang Ref

Hello Dave,

I’ve read about the destructor in the language reference. The constructor is used by me to to understand, when the object is destroyed. So I set a breakpoint into the dlgUser destructor for that.

With the dlgUser.Close statement in the code above, the destructor is called when the scope of the “Sub Action” is left.

But if the statement (dlgUser.Close) is missing , the destructor is not called when the scope of the “Sub Action” is left. My question is: Is the dlgUser object really destroyed in this case? The Destructor isn’t called!

Don’t worry about it…
The visibile event is for CUSTOM CLASSES… destructors for windows are in the SUPER

Yes the window destructor IS called… when the ShowModal exits.

You have to call dlg.Close() if in the dialog itself you are only calling Hide() to proceed with the code after dlg.ShowModal.
If you call Self.Close() within the dialog, you don’t call dlg.Close() again in the caller.

Yes,
and when either Self.Close within the dialog nor dlgClose in the caller is called, I had expected that the dlgUser.Destructor is called in the moment when the “Sub Action” is left. But this doesn’t happen and so I wondered why the dlgUser.Destructor isn’t called when the local object lost the scope.

The window will not seize to exists after the end of your Action method if you have not called Close(). The Close method closes all controls in the window and sets them to Nil. Only then the window is in a state where it can be released from memory.

Hello Eli,

does that mean that the window is not released from memory when the close method is not called, even when the Action method ends?

A SHOWMODAL window is closed and released as soon as you Close that window… you cannot leave a showmodal open, hence the definition of “MODAL”

Therefore… as soon as you return to the method that called the SHOWMODAL window… it has been closed, and released…

A window that you HIDE and use SHOW to display, remains “open” until you CLOSE it… as soon as you CLOSE it… it too is released.

At no time do you ever need to call or worry about a window destructor…

Sub Action
Dim dlgUser As New WinUser

dlgUser.ShowModal

// When you get to here... dlgUser has been closed already...

dlgUser.Close // not required as the window was closed when showmodal was done
End Sub <---- In anycase dlgUser is destroyed here, as it has gone out of scope when the action completes 

Hello Dave,
sorry for my bad english, but I have never intended to call a Destructor myself. The Destructor is used to understand (via breakpoint) when the object is destroyed.

Therefore… as soon as you return to the method that called the SHOWMODAL window… it has been closed, and released…

In general this statement is not correct. In the sample above I returned from the dlgUser to the caller method and could access from the caller method the objects Textfields. So dlgUsercould not be released at this time (it is hidden).

A window that you HIDE and use SHOW to display, remains “open” until you CLOSE it… as soon as you CLOSE it… it too is >>released.

Ok, when the window is closed with the close-method it is released from memory.

But what happens to the dlgUser window, when the close comand for this window is not called and the caller method ends.
Is the dlgUser window then released from memory or not?

Eli Ott says in his last message, that the window doesn’t exist anymore. An I deduce from his complete answer, that the window is not released from memory, because the close method was not called.

Look at Window.ImplicitInstance which is by default TRUE

Simply attempting to refer to any property of the window creates an instance of it …
The window was closed at the end of showmodal… if you put in a breakpoint and refereed to it again… it created it again

So my statements stand. Unless someone else can explain an alternative.

imply attempting to refer to any property of the window creates an instance of it …

It may create an instance but not a copy. And that is what I got in the sample above. Have you tried it? I got exactly the values from the textfields that are entered in the User dialog(dlgUser.blnSaveButtonClicked, dlgUser.txtName.Text, dlgUser.txtMedikament.Text). Thats not possible with a new instance!

The Save-Button of the dlgUser window calls the hide method of the window.

// Real live and working method
Sub Action
Dim dlgUser As New WinUser

dlgUser.ShowModal

If dlgUser.blnSaveButtonClicked = True Then
Self.Users.AddUser(dlgUser.txtName.Text, dlgUser.txtMedikament.Text)
End If

dlgUser.Close
End Sub

Have you switched ImplicitInstance on WinUser to False in the IDE as Dave wrote? If not the window will stay forever. Unfortunately the default for ImplicitInstance is True.

@Eli Ott
Yes, the ImplicitInstance is set to true.

You answered my question in your previous post. Together with my study I think I have the answer.

Many thanks to you and Dave.

Dave is correct, the window should be destructed when it is dismissed. But that is a bit simplistic. The destructor is called when and only when the reference count to the window falls to zero. That is the main thing.

If you are finding that the destructor is not being called, it means that you are keeping a reference somewhere. Most likely, you have a circular reference within the window itself.

And, no, this all has nothing to do with Implicit instance.