Cocoa: CALayer: continuous redraw of a view continuously increases allocated memory

903 Views Asked by At

I have a problem of memory allocation (not leak anyway).

My program has a custom Window with a custom View containing a TextField, an ImageView and a Shadow. Let's say that every 1 second I programmatically update the value of the TextField using [myTextField setStringValue:@"actual string"].

Obviously, every time the TextField get changed, the view is redrawn.

If I look in Activity Monitor I see that every time the TextField is updated, and therefore the view is redrawn, the allocated memory increases. The ImageView isn't supposed to change. If I comment the line with setStringValue, the program runs without increasing memory at all. (See Update 4.)

Note that Instruments does not report memory leak or unreleased object and View is autorelease'd.

What can cause this?

UPDATE

I post a simplified version of the actual code:

.h

CustomTextField *myTextField;
int level;

@interface Dummy : NSObject {

NSString *level_string;
NSTimer *timer;

}

@end

.m

@implementation Dummy

- (void)awakeFromNib
{
// ...
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 
                                                  target:self 
                                                selector:@selector(changestring:) 
                                                userInfo:nil
                                                 repeats:YES];

// ...

}


-(void)changestring:(NSTimer *)timer
{
        level++;
        level_string = [[NSString alloc] initWithFormat:@"%i",level];
        [myTextField setStringValue:level_string];
        [level_string release];

}

Where CustomTextField is a NSTextField class.

I don't know if what I'm going to add is important, anyway the custom window, the custom view and the custom textfield are defined and init programmatically in the code and they are not instantiated in interface builder.

UPDATE 2

I was wrong! Even if I comment setStringValue the memory still increases..a lot less, but it still increases.. The strange fact is that Instruments, in any case, reports a size of "Living Object" which remains constant and no leak is reported.

What is happening?

UPDATE 3

I have just used the amazing heapshots feature of Instruments and this is the result.

The memory increase that I see in activity monitor (which is of the order of thousands of kilobytes after few minutes) where does come from?

UPDATE 4

I think I have found what is causing the problem but I'm not able to solve it.

The View has a TextField, an ImageView and a Shadow. To make them appearing correctly on screen without glitches I have added [view setWantsLayer:YES]. If I comment this line, the memory allocation problem is definitely solved.

Now, as long as I need to use that command, how can I do? Am I supposed to release something related with Core Animation? Note that the only one command related with Core Animation is the above one.

2

There are 2 best solutions below

1
On

What setStringValue: in CustomTextField does? My guess is that you're retaining passed string without releasing old value in your setter.

8
On

Have a look at session 311 - Advanced Memory Analysis with Instruments of WWDC10 session videos. How to download these videos can be found here.

It could be that you see abandoned memory or that there is some caching going on. What is happening if you trigger a low memory warning in the simulator? Goes the memory down?

Also try using the heapshot feature of Instruments: make a heapshot, update the string and show which objects have been created. In the session video mentioned above the procedure is demonstrated.

EDIT: What I forgot: if you cannot figure out what is going on and you can show this behavior of increasing memory allocation without releasing it if a low memory warning is triggered in a simple example application, file a bug at Apple's bugreporter sending in this sample project and an Instruments trace file to show what is going on.

EDIT2: I was wrong assuming that this problem was on Mac OS X. The same principles apply for a Mac OS X application as well. Memory management in Cocoa is almost the same on the iPhone and the Mac. The biggest difference is that on the Mac there is garbage collection available. Of course on the Mac there are no low memory warnings, so leave that out.