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 (See Update 4.)setStringValue
, the program runs without increasing memory at all.
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.
What setStringValue: in CustomTextField does? My guess is that you're retaining passed string without releasing old value in your setter.