View Full Version : gc() kills Eventhandlers

04 April 2008, 12:56 PM

I have a strange problem. I created a dotnet based UI with windows.forms. Basicaly like this:

-- Global Buttonhandler
global btnhandler
fn btnhandler A B = (print

-- Create Form
hForm = dotNetObject "System.Windows.Forms.Form"
hForm.Size = dotNetObject "System.Drawing.Size" 310 335
hForm.Text = ".Net 2.0 Form with TextBox"
hForm.TopMost = true
FormBorderStyle = dotNetClass "System.Windows.Forms.FormBorderStyle"
hForm.FormBorderStyle = FormBorderStyle.FixedDialog
hForm.ShowInTaskbar = false
hForm.MinimizeBox = false
hForm.MaximizeBox = false

--Create Layout
Dockstyle = dotnetclass "System.Windows.Forms.DockStyle"
tv = dotNetObject "System.Windows.Forms.FlowLayoutPanel"
tv.Dock = DockStyle.Fill
tv.BorderStyle = (dotNetClass "System.Windows.Forms.BorderStyle").None
tv.Autoscroll = true

-- Read out Values from Database and generate Buttons
buttonnames = #("These","are","my","buttons") -- Hardcoded for demonstration
for b in buttonnames do
btn = (dotNetObject "System.Windows.Forms.Button")
btn.text = b = b
dotnet.addeventhandler btn "click" btnhandler

-- Set appropriate Form background color
maxBackColor = colorMan.getColor #background
Color = dotNetClass "System.Drawing.Color"
hForm.BackColor = Color.FromArgb (maxBackColor[1] * 255.0f) (maxBackColor[2] * 255.0f) (maxBackColor[3] * 255.0f)
-- Show application Form
hApp = dotNetClass "System.Windows.Forms.Application"
hApp.Run hForm


Just more complicated with a lot of buttons, tabs, layouts etc. I am reading out an SQL Database and then I generate several buttons dynamically. Just like I did in the example above. The buttons works until you type gc() in the maxscript listener. Or if 3dsmax thinks it's time to do that. How can I prevent that the garbage collector deletes the button handlers?

Best regards,


04 April 2008, 04:21 PM

just for the records. I found a way to protect the button handler from the garbage collector. I declare a global array "buttonarray = #()" and then I add all dynamically generated buttons to that array with "append buttonarray btn". This way the gc() does not destroy the handles.

Best regards,


04 April 2008, 11:15 PM
Hi Dieter,

Take a look at dotNetMXSValue in the MXS help. I think this is the instrument maxscript provides to avoid the issue you're having.


04 April 2008, 02:01 PM
Thanks for the pointer. Seems to be the right solution.

Best regards,


CGTalk Moderation
04 April 2008, 02:01 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.