.Net combobox and garbage collection

.Net combobox and garbage collection

Anonymous
Not applicable
829 Views
5 Replies
Message 1 of 6

.Net combobox and garbage collection

Anonymous
Not applicable
Hi,

i'm having trouble with my .net comboboxes. I use pieces of this code in a script i'm making. Essentially i want to use a .Net combobox on a .Net form. I do garbagecollection at the end here, but in my script it's put somewhere inside the script. So:

theForm = dotNetObject "form"
myComboBox = dotNetObject "comboBox"
theForm.Controls.Add myComboBox
theForm.Show()
gc()


When i run this code once everything looks fine, when i run it twice i get an error dialog: "An unknown error occurred while MAXScript was performing garbage collection...". After that, just evaluation gc() will produce the same mesagebox. To get rid of this error i just have to restart max eventually. On my machine this behaviour is consistent. A button won't throw this error, just comboboxes. Can anyone confirm this? Better yet: does anyone have an idea to solve this?

greetings,

Klaas
0 Likes
830 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable
Is there a need to call gc() would be my first question. It's my understanding (from the help file) that it does not need to be explicitly called. And from the snippet, I see no reason to call it. Also, and I could be wrong about this, theForm, while in operation, will perform it's own (dot.net) garbage collection. Either way, if it works without putting a gc() at the end, I'd say leave it out.

MP
0 Likes
Message 3 of 6

Anonymous
Not applicable
You're absolutely right, there's no need to call gc(). In the meanwhile i can assume max will use its internal garbage collection at some point. So that's why i call it manually here.
My point is: why does garbage collection and comboboxes cause this error, it doesn't make sense to me. The rest of my interface is fine when i leave out the comboboxes.
The error also occurs when max internal garbage collection is executed i learned. The code snippet below creates strings. Executing the function two or three times triggers the garbage collection at my computer. So when i create the form with the combobox from the first post twice and then either manual garbage collect or trigger max to do it itself: i get the error.

fn makeMaxGarbageCollect loopCount =
(
memStart = heapfree
local str1 = ""
for i = 1 to loopCount do str1 = (i as string)
memEnd = heapfree
format "memory used: %\n" (memStart - memEnd)
format "memory left: %\n" memEnd
)

makeMaxGarbageCollect 200000


It'd also help me if someone could reproduce this behaviour.

Klaas
0 Likes
Message 4 of 6

Anonymous
Not applicable
I'd be more then happy to test out some code, but what I see so far (in your first post) doesn't give me a lot to go on. I'd also point out that by using the form object, certain requirements are put on you (there's no autogen code to save your butt). Working with .net objects is really a double edge sword, if you were writing the code in C# or vb, there would be a lot more code to show the form then just form.show (whether coded by you or the autogen code). I hope you understand what I mean, just because you can create .net objects - doesn't mean they will work the same as in an IDE - and more often then not, will require (not the right word but...) "hidden" parameters usually taken care of by the IDE etc.

Just as another line of thinking, is there any reason you're using the Form object instead of a RollOut, not that it matters, just asking. But I'm pretty sure there's more to using a .Form as the main interface then meets the eye.

One more line of thought - the same happens here, button works, combo doesn't. Button is inherited from Control, Combo is inherited from ListControl... just sayin...

Also, I'm keenly aware that I could be totally off track.

MP
0 Likes
Message 5 of 6

Anonymous
Not applicable
Well thanks for the testing.

I must admit i don't understand some of your explanation. I guess you're telling me maxscript and .net are hiding/automating lots of code. The problem i'm having with my comboboxes could be caused by some of this hidden code.

The other thing: there's actually no compelling reason for me to use the .net form. I'm using lots of .net controls, drag/dropping, xml reading/writing so i guessed a .net form would be an appropriate home for them. A rollout could work out as well. I could test that by adding all my controls to a .net panel and adding that panel to a rollout. That way i wouldn't have to rewrite my script drastically.

Thanks again for confirming the issue, i'll be testing the rollout next evening.

Klaas
0 Likes
Message 6 of 6

Anonymous
Not applicable
It's not that maxscript is hiding anything, it's the way it supports .net. When you work in an IDE like Visual Studio, and create forms and controls - there's a lot going on in the background. Some code is auto generated by the IDE and compiler to make all the .net stuff work properly. In maxscipt, none of that happens - in most cases, an object can be created easily, without any additional code, but for some things it's a little more involved. Not everything in .net can be created properly simply by instancing it's class in maxscript.

As for the combobox, I'm pretty sure it had more to do with the form and how it happens to handle the combo internally. Maxscript also has its own combobox (not with the same functionality of course) - but another option if you get stuck.

Here's some links that might help too
http://forums.cgsociety.org/showthread.php?f=98&t=551473 --> DeFacto thread for MXS+.Net
http://www.scriptspot.com/bobo/mxs9/dotNet/DotNetControls.html --> Quick Reference from Borislav Petrov (Bobo)


MP
0 Likes