windows not getting released

Hello,
Xojo 15v4 (but it seems the issue goes back at least one year).
I create a new project with two windows (Document). One is set as visible, the other one as invisible.
The first window contains a push button whose action event says: Window2.show

Once Window2 is open, Activity Monitor shows that the Memory Usage has raised by 1 M.
I close Window2, but Memory Usage does not decrease.

And each time I open window2, Memory Usage increases by 1 M, and closing it, Memory never decreases.

Now, I have an app that every three minutes opens an Activity window, and after a while it closes it.
After several hours, the Memory Usage that at launch was 18 M, shows to be 90 or more M.

Is there a way to release all those megas? Thanks.

If you open Window2 using Window2.show then you probably have ‘implicit instantiation’ turned on.
Thats where Xojo creates a window of the same name as the window class.

It may possibly never be set to nil.

But if memory goes up every time, what do you do in the activate or open events to use the memory?

Try this:
Add a property to the app called mActivityWindow

When you would have used Window2.show, use this instead:

mActivityWindow = new Window2 mActivityWindow.show

By setting the mActivityWindow to a new object, you will release the usage count from the previously used copy, and garbage collection should release the memory.

Tried it, but memory still increases.
BTW, in the real app, I need implicitInstantiation in order to refer to methods and functions between the two windows.
But in the new project I was referring to, window1 contains only a button, while window2 does not contain anything anywhere.

Actually, not true. :wink:
You can have global / app level variable of type Window1 and Window2

If mActivityWindow is a global property, then no matter how many times it is created and destroyed, you can access the properties of mActivityWindow
in exactly the same way you can with
Window2

To extremes… you could have 25 Window2 objects in an array!
Each one of them can have their properties and methods accessed …

myWindow2Array(6).property1 = 2827.65

Anything??

You could also just hide the window instead of closing it every time, but you’re right, it shouldn’t leak memory.

I suggest posting your sample so others can see what you’ve done or file a bug report with your sample so we can look at it.

@ Greg; here is a link
https://www.dropbox.com/s/ra9cuj73swu4l9f/WindowTest.xojo_binary_project?dl=0

@ Jeff
OK, I agree. Yet, even with no properties nor methods nor objects, memory does not decrease; instead it increases.

Tested.
Yes it does.
Even setting the window to nil…
About 1Mb every time you use show.

You might want to add info to this feedback entry: <https://xojo.com/issue/34956>

Just add this to the close event of window2:

Declare Sub release Lib "Foundation" Selector "release" (receiver As Ptr) release(Ptr(Handle))

Brilliant, Eli! Would you mind adding that to the feedback thread as solution? Would be nice to have it fixed in general.

[quote=238731:@Eli Ott]Just add this to the close event of window2:

Declare Sub release Lib "Foundation" Selector "release" (receiver As Ptr) release(Ptr(Handle))[/quote]

This will cause mysterious crashes once the framework bug is fixed.

Then make it
#If XojoVersion < 2016 and TargetMacOS
…
#Endif

EDIT: I put the fix into Feedback. Great start for the year, I was concerned about using windows extensively because of that and installed rather complicated window instance handlers in my projects.

Of course it will. And releasing is currently the only way to solve the problem as Xojo hasn’t addressed this. No offense, but memory leaks should always have top priority and should be fixed immediately. Note that the error was reported in August 2014 and is still not fixed, so one can assume that it will take some time until it will.

Happy to know that the issue was not something confined to what happened to me.
And happy for the temporary solution (Eli and Ulrich).

… and you might have seen it’s marked fixed in Feedback now, which makes me think the condition in the code above won’t have to be modified and the fix will be in 2016r1.

Assuming the fix didn’t cause any unexpected regressions, it should be in 2016r1.

I hope it fixes also this corollary problem:
let us assume that before opening window2 memory usage is 20MB;
when the second window contains a pagepanel where the first pane holds 3 objects, the second 7 objects etc, then moving from one pane to panes holding more objects, Memory Usage increases, and after closing the window, memory usage is usually more than 20MB (according to the number of objects in a given pane, it could be 24MB or more).
At present this happens even using:
Declare Sub release Lib “Foundation” Selector “release” (receiver As Ptr)
release(Ptr(Handle))

[quote=239337:@Carlo Rubini]I hope it fixes also this corollary problem:
let us assume that before opening window2 memory usage is 20MB;
when the second window contains a pagepanel where the first pane holds 3 objects, the second 7 objects etc, then moving from one pane to panes holding more objects, Memory Usage increases, and after closing the window, memory usage is usually more than 20MB (according to the number of objects in a given pane, it could be 24MB or more).
At present this happens even using:
Declare Sub release Lib “Foundation” Selector “release” (receiver As Ptr)
release(Ptr(Handle))[/quote]

This sounds unrelated, so you should file a bug report with an example project.

Submitted: #42046

I too think this is unrelated. During development from time to time I go through the RuntimeObject collection before quitting the application to see if there are objects lingering around. If everything is released properly there should be less than ten objects plus the menu items.

During such a check years ago (still RB) I noticed that sometimes when a window is complex and contains PagePanels (or TabPanels) plus ContainerControls, the window is never released. Somehow the “closing cascade” didn’t work and some control on a container on a page panel on a container (or similar) never were closed (the implemented close event was not called). Therefore since then I have implemented in all my container, page panel and also canvas subclasses – meaning in everything which can become some sort of parent – the Close event and in there I call the Close method on all children.