0

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.

Andrea3000
  • 1,018
  • 1
  • 11
  • 26

2 Answers2

2

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.

Community
  • 1
  • 1
GorillaPatch
  • 5,007
  • 1
  • 39
  • 56
  • 1
    This is a Cocoa question, not Cocoa Touch, there's no simulator for Mac apps. – Rob Keniger Feb 21 '11 at 02:02
  • Also no such thing as a low-memory warning. – Peter Hosey Feb 21 '11 at 07:07
  • @GorillaPatch Thank you for the answer. I'm looking at the video you mentioned but, as noted above, I'm building a Cocoa app for OSX and therefore I can't use the simulator. The heapshot feature of Instruments seems interesting but I'm really new to Cocoa and I don't know how to do that at the moment..I hope to learn something from that video. Thank you – Andrea3000 Feb 21 '11 at 08:02
  • @Andrea3000 Yes I was wrong in assuming you are developing for the iPhone. I edited the answer to clearify some differences between these two platforms. – GorillaPatch Feb 21 '11 at 08:52
  • @GorillaPatch: Thank you for the explanation, as soon as I'll look the video till the end I will try to do what you suggested. Anyway, about garbage collection, I replied you in the comment to the first question. – Andrea3000 Feb 21 '11 at 11:29
  • @GorillaPatch: I have updated the first post adding a snapshot in which I did exactly what you suggested and the result is..let's say..unexpected. Have you got any hint on that? – Andrea3000 Feb 22 '11 at 19:59
  • First a question remains: you are right that is kind of ugly, but are you really harmed by that little wasted memory on a multi-GB RAM desktop computer? AFAIK that behavior is not correct and no additional RAM should be allocated. If you really want to go to the root of the problem, try making a sample project consisting of nothing but a textField and make sure this behavior exists in this test setup. Make the same heapshot with it and file a bug at apple, including the sample project and your saved instruments output. Meanwhile you could look at the call stack which function allocated the RAM – GorillaPatch Feb 23 '11 at 08:23
  • @GorillaPatch: Thank you for your answer, really appreciated. I have found what is causing the problem but I can't find the solution. I have updated the original post and I'm still googling for a solution – Andrea3000 Feb 23 '11 at 21:32
0

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

tundrabot
  • 341
  • 2
  • 3
  • Trying to simplify the code I forgot to say that the string `level_string` is `autorelease`'d. For what is about `setStringValue:` it's not declared in `CustomTextField`, I'm not overwriting this method..it has its default behavior, is it incorrect? – Andrea3000 Feb 21 '11 at 12:26