Memory leaks with Script.RegisterEvent method

This forum is for questions / discussions regarding development of addons / tweaks for MediaMonkey.

Moderators: Gurus, Addon Administrators

Posts: 3141
Joined: Thu Jun 08, 2006 7:40 pm

Memory leaks with Script.RegisterEvent method

Post by ZvezdanD »

Just for completeness... DeathAxe was right - there are a memory leaks with my script, but he was wrong for two reasons: first, it is not RegExp which has memory leaks, but new 2.0 version of Magic Nodes, and second, not releasing of RegExp and other objects on the end of some function is not a reason for memory leaks. Script.RegisterEvent is a problem. So, I suppose that many scripts which use this method have memory leaks as well.

I reported this to Jiri. Here is my report:
I have scripts with dialog boxes with many controls and almost all of this controls have registered event handlers. I just recently noticed memory leaks when I open/close dialog boxes several times. Well, I think that solution is relatively simple: I just need to unregister all those event handlers when I closing dialog boxes (this should be mentioned in the wiki).

However, there is one thing which could not be resolved in such way: Option sheet. I have registered event handlers for some controls from the option sheet in the InitProcedure function and I could put UnRegisterEvents method in the SaveProcedure function, but I get memory leaks when I click on the Cancel button because none of the script code is executed when user clicks on that button.

Unfortunately, with some scripts (e.g. Magic Nodes) I get memory leaks even if I execute UnRegisterEvents method after click on the OK button of the Options dialog. :(

Do you have some suggestion for this problem?
I got a short answer to this mail, but my successive e-mails have not any response:
I have installed TClockEx, one small program which could display free RAM, but I tried also with Task Manager and Performance Monitor. Memory is leaking and it is not returned back nevertheless how long I wait. If I open/close dialog boxes many times I could easily stay with 0 MB of memory (I have MM in some rather old machine with 512 MB of RAM). I am using FreeRAM program which could defragment RAM and return back unoccupied memory (it is working as resident in background), so I have not any problem with memory leaking, but I worry about other users which use my scripts. Well, not only my scripts, since this problem is general with all MM scripts which have RegisterEvent method.

However, the fact is next: when I put UnRegisterEvents in the end of some dialog procedure, i.e. after Form.ShowModal, memory is returned to same amount as it was before opening this dialog box not matter how many time I open and close it, but if I don't put UnRegisterEvents on the end of such procedure, the free memory is decreasing with every open/close.

Instead to write some sample script, you could try my existing scripts, most notably Magic Nodes which has most controls and event handlers of all and because of that memory is mostly decreasing with it (I suppose so). The last public version still don't have UnRegisterEvents on the end of any dialog procedure so you will easily notice memory leaks. Install script from, open/close the Edit / Magic Nodes Settings dialog several times and look at the free memory with Task Manager. Now, you could put Script.UnRegisterAllEvents on the end of the SettingsDialog procedure in the line 6130 and note amount of free memory again - you will see a difference.

The next test which you could try with the Magic Nodes script is with the Tools / Options dialog (click on the Magic Nodes Options just to be sure that its sheet is displayed). If you open/close this dialog several times, you will see memory leaks again. But now, putting of Script.UnRegisterAllEvents on the end of the SaveOptionSheet procedure (line 10235) has not any effect, even if you click on the OK button. As I already described, click on the Cancel button is not covered by user scripts so there is not any chance to unregister event handlers in such case.
One more unanswered mail:
This is now a real problem... I just discovered that Script.UnRegisterEvents don't resolve a problem with memory leaks, but only Script.UnRegisterAllEvents. I suppose that a reason for this is because I am unregistering event handlers for controls from a form which is already closed, i.e. after Form.ShowModal is executed.

You may wonder why this is a problem if I could use Script.UnRegisterAllEvents. Well, with the Magic Nodes Settings dialog I could create new nodes and when I do this I execute next lines:
Script.RegisterEvent CustomNode, "OnFillChildren", "FillCustomNode"
Script.RegisterEvent CustomNode, "OnNodeEdited", "EditCustomNode"
Script.RegisterEvent CustomNode, "OnNodeEditText", "CustomNodeEditText"
Script.RegisterEvent CustomNode, "OnCanEditNode", "CanEditCustomNode"
Script.RegisterEvent CustomNode, "OnFillTracks", "FillCustomLeaf"

So, when I apply Script.UnRegisterAllEvents I am unregistering not only event handlers for controls from a form but also all of this event handlers for newly created nodes. This means that when I close mentioned dialog, all newly created nodes are disappearing!

By the way, I just tested: memory leaks with the Magic Nodes Settings are about 2-3 MB per opening/closing of this dialog, which is not negligible if you ask me. When I put Script.UnRegisterAllEvents on the end of dialog procedure there is not any memory leaks, as I explained in my previous mail.
As I said in another forum, after spending so much time trying to detect and resolve such problem, and writing report with explanation about it, I think that I deserve to get at least short information about its status. Just one sentence could satisfy me.
Magic Nodes 4.3.3 / 5.2 RegExp Find & Replace 4.4.9 / 5.2  Invert Selection/Select None 1.5.1  Export/Create Playlists for Child Nodes 4.1.1 / 5.4.1  Expand Child Nodes/Expand All 1.1.2  Event Logger 2.7  Filtered Statistics Report 1.6  Track Redirection & Synchronization 3.4.2  Restore/Synchronize Database 3.1.8 / 4.0.1  Find Currently Playing Track 1.3.2  Queue List 1.2.1  Add to Library on Play 1.0.1  Tree Report for Child Nodes 1.1.1  Update Location of Files in Database 1.4.5 / 2.3  Inherit Child Playlists 1.0.3  Add Currently Playing/Selected Track(s) to Playlist 1.2
Posts: 1321
Joined: Thu Jun 15, 2006 3:26 pm
Location: Geelong, Victoria, Australia

Re: Memory leaks with Script.RegisterEvent method

Post by MoDementia »

I'm not very surprised about this information, I have also had trouble getting rid of forms completely; there is something not right for sure.

As for options sheets, these were never included with the events system so the problems with these are unavoidable :(
Post Reply