Notifications
Clear all

[Closed] gc() kills Eventhandlers

Hi,

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 A.name) 			
	
	-- 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
		btn.name = b
		dotnet.addeventhandler btn "click" btnhandler
		tv.Controls.Add(btn)
	)
	
	-- 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)
	hForm.Controls.Add(tv)
	-- 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,

Dieter

3 Replies

ok,

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,

Dieter

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.

cheers,
o

Thanks for the pointer. Seems to be the right solution.

Best regards,

Dieter