[Mono-list] Another Memory Leaking Question

Stifu stifu at free.fr
Wed Sep 8 10:15:10 EDT 2010


Well, the code is quite simple...

I don't think that would change anything as far as your memory leak goes,
but you could write it this way:

using (NSString val = new NSString(string.Format("Clicked {0} times", ++i)))
{
	label.StringValue = val;
}

This is a habit I have, at least. I think it looks better, and it's also
safer (the object is disposed even if an exception is raised).

I doubt it's the string.Format part that causes problems, but just in case,
you could replace it with a hardcoded string just to test if it changes
anything... and by the way, I'm not familiar with the NSString class, so
there may be quirks or specificities I'm unaware of.

Oh, and one last random idea: from what I understand, when setting the
StringValue property of the label, the NSString object is cloned rather than
just referenced (else your program would cause exceptions, as you'd be
disposing of an object that's still needed by the label). If that's the
case, that would mean the StringValue is a different object from the val
one, and so you could try to dispose of the StringValue, too.

Like:

(...)

label.StringValue.Dispose(); // Assuming StringValue can never be null, else
initialize it beforehand
using (NSString val = new NSString(string.Format("Clicked {0} times", ++i)))
{
    label.StringValue = val;
}

(...)

This might help the GC... *shrugs*


flohei wrote:
> 
> Hi Stifu,
> 
> this is what I'm doing when the button get's clicked:
> 
> 		int i = 0;
> 		
> 		partial void buttonClicked (NSObject sender) {
> 			for (int i = 0; i < 20000; i++) {
> 				NSString val = new NSString(string.Format("Clicked {0} times", ++i));
> 				label.StringValue = val;
> 				val.Dispose();
> 			}
> 		}
> 
> I did not try it using only .NET or enabling SGen yet. I'm using a trunk
> build of MonoDevelop that can be found 
> http://monodevelop.com/Download/Trunk_Builds here .
> 
> I'm trying to figure out if MonoMac is the right solution for an upcoming
> project where we have to build a native Mac UI for an existing .NET
> application.
> 
> Best
> –f
> 

-- 
View this message in context: http://mono.1490590.n4.nabble.com/Another-Memory-Leaking-Question-tp2531261p2531351.html
Sent from the Mono - General mailing list archive at Nabble.com.


More information about the Mono-list mailing list