[Mono-osx] Memory Leak ?

Miguel de Icaza miguel at novell.com
Sun Jan 9 21:23:37 EST 2011


Hello,

            t = NSTimer.CreateRepeatingScheduledTimer(1,delegate {
>                 txtClock.StringValue = DateTime.Now.ToLongTimeString();
>             });
>
>
The problem is that you are starting a timer before the UI has been
initialized and the value of the txtClock property has a valid value.

What happens when you build an object from a NIB file is that:

1. The constructor is called, you can do some bare-bone initialization here.
2. The NIB is loaded, objects deserialized.
3. AwakeFromNib is invoked (or something like that, I cant remember the
method now)

So none of the properties that are mapped to outlets have valid values until
Step 3.   So make sure that nothing touches those fields until then.

Miguel
initializer your time in the AwakeFromNib override instead, which is when
the UI has been loaded.   Before that the txtClock is still null.

Miguel

>
>
> I get this error:
>
> Unhandled Exception: System.NullReferenceException: Object reference not
> set to an instance of an object
>   at UglyClock.MainWindowController.<AwakeFromNib>m__0 () [0x00000] in
> <filename unknown>:0
>   at MonoMac.Foundation.NSActionDispatcher.Apply () [0x00000] in <filename
> unknown>:0
>   at (wrapper native-to-managed)
> object:[MonoMac.Foundation.NSActionDispatcher:Void Apply()]
> (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector)
>   at (wrapper managed-to-native)
> MonoMac.AppKit.NSApplication:NSApplicationMain (int,string[])
>   at MonoMac.AppKit.NSApplication.Main (System.String[] args) [0x00000] in
> <filename unknown>:0
>   at UglyClock.MainClass.Main (System.String[] args) [0x00000] in <filename
> unknown>:0
>
>
> My code was working, so I know the objects are all wired correctly. Humm,
> seems it can't find txtClock to update this way.  If I change the code to
> this it works great (So, I know the delegate is working):
>
>
>             t = NSTimer.CreateRepeatingScheduledTimer(1,delegate {
>                 Console.WriteLine(DateTime.Now.ToLongTimeString());
>                 //txtClock.StringValue = DateTime.Now.ToLongTimeString();
>             });
>
> You know what could be wrong, sorry I'm a total noob, this is my 3rd day
> trying to use this.
>
>    hazlema <hazlema at gmail.com>
> January 9, 2011 4:51 PM
>
> Ahh, ok. Thanks
>  ------------------------------
>
>    kjpou <kjpou at pt.lu>
> January 9, 2011 3:33 PM
>
> Mathew
>
> It is not a memory leak.
>
> What you have to do is use a NSTimer.
>
> NSTimer t;
>
>             t = NSTimer.CreateRepeatingScheduledTimer(1,delegate {
>                  txtClock.StringValue = DateTime.Now.ToLongTimeString();
>             });
>
>
> When you use a .net timer it runs outside NSApplication loop.  The NSTimer
> will run within the application loop allowing Cocoa to update the field
> value correctly.
>
> I plan on writing an article about that in a couple of days.
>
>
> ------------------------------
>
>    Matthew Hazlett <hazlema at gmail.com>
> January 9, 2011 1:52 PM
>
>
> I made a terribly simple application, on the MainWindow I put a label then
> in the code I had it update the label every 1000 of a second.
>
>         ...
>        Timer t = new Timer(1000);
>         ...
>
>         public override void AwakeFromNib ()
>         {
>             base.AwakeFromNib ();
>
>             t.Elapsed += delegate {
>
>                 txtClock.StringValue = DateTime.Now.ToLongTimeString();
>
>             };
>
>             t.Start();
>         }
>
> But it keeps throwing warning errors at me for every tick, what am I
> missing and how do I fix it?
> *2011-01-09 13:47:30.563 UglyClock[79395:7103] ***
> __NSAutoreleaseNoPool(): Object 0x540f80 of class NSCFString autoreleased
> with no pool in place - just leaking*
>
>
>
> _______________________________________________
> Mono-osx mailing list
> Mono-osx at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-osx
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-osx/attachments/20110109/b305cf7b/attachment.html 


More information about the Mono-osx mailing list