Note that there are some explanatory texts on larger screens.

plurals
  1. POUIView and NSTimer not releasing memory
    text
    copied!<p>I have a line of UILabel text in a UIView which is regularly updated via NSTimer. This code is supposed to write a status item near the bottom of the screen every so often. The data comes from outside of its control.</p> <p>My app runs out of memory really fast because it seems the UILabel is not being released. It seems that dealloc is never called.</p> <p>Here is a very compressed version of my code (error checking etc removed for clarity.):</p> <p>File:SbarLeakAppDelegate.h</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import "Status.h" @interface SbarLeakAppDelegate : NSObject { UIWindow *window; Model *model; } @end </code></pre> <p>File:SbarLeakAppDelegate.m</p> <pre><code>#import "SbarLeakAppDelegate.h" @implementation SbarLeakAppDelegate - (void)applicationDidFinishLaunching:(UIApplication *)application { model=[Model sharedModel]; Status * st=[[Status alloc] initWithFrame:CGRectMake(0.0, 420.0, 320.0, 12.0)]; [window addSubview:st]; [st release]; [window makeKeyAndVisible]; } - (void)dealloc { [window release]; [super dealloc]; } @end </code></pre> <p>File:Status.h</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import "Model.h" @interface Status : UIView { Model *model; UILabel * title; } @end </code></pre> <p>File:Status.m This is the where the problem lies. UILabel just does not seem to be released, and quite possible the string as well.</p> <pre><code>#import "Status.h" @implementation Status - (id)initWithFrame:(CGRect)frame { self=[super initWithFrame:frame]; model=[Model sharedModel]; [NSTimer scheduledTimerWithTimeInterval:.200 target:self selector:@selector(setNeedsDisplay) userInfo:nil repeats:YES]; return self; } - (void)drawRect:(CGRect)rect { title =[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 12.0f)]; title.text = [NSString stringWithFormat:@"Tick %d", [model n]] ; [self addSubview:title]; [title release]; } - (void)dealloc { [super dealloc]; } @end </code></pre> <p>File: Model.h (this and the next are the data sources, so included only for completeness.) All it does is update a counter every second.</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface Model : NSObject { int n; } @property int n; +(Model *) sharedModel; -(void) inc; @end </code></pre> <p>File: Model.m</p> <pre><code>#import "Model.h" @implementation Model static Model * sharedModel = nil; + (Model *) sharedModel { if (sharedModel == nil) sharedModel = [[self alloc] init]; return sharedModel; } @synthesize n; -(id) init { self=[super init]; [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(inc) userInfo:nil repeats:YES]; return self; } -(void) inc { n++; } @end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload