English 中文(简体)
iPhone memory management problems
原标题:

I detach a thread calling my method which has a while-loop. Even though I have them marked as autoreleasepool, I release the objects manually, since the while-loop can continue on for a some time.

The problem is that after a while, the app crashes due to memory problems. If I look in Instruments, I can see a huge pile of NSStrings allocated and a stairway to heaven is created in the graph. What have I failed to release?

while (keepGettingScores)  
{  
    NSString *jsonString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];  
    NSDictionary *json = [jsonString JSONValue];  
    [jsonString release];   

    NSMutableArray *scores = [[NSMutableArray alloc] init];  
    [scores setArray:(NSMutableArray*)[[jsonString JSONValue] objectForKey:@"scores"]];

    NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"totalScore" ascending:NO];  
    [scores sortUsingDescriptors:[NSArray arrayWithObject:sorter]];  
    [sorter release];  

    [self performSelectorOnMainThread:@selector(updatePlayerTable:) withObject:scores waitUntilDone:NO];  
    [scores release];  

    [NSThread sleepForTimeInterval:1.0];  
}  
问题回答

I don t see anything glaring, could there be an issue under the hood in your JSON library?

Are you draining your pool after your thread is finished executing?

You need to create an NSAutoreleasePool and call its drain method when your thread is finished executing.

In one of my projects, I had a thread that needed to create a lot of autorelease objects and found it useful to periodically drain the pool as the thread was running.

- (void)doStuff:(NSObject *)parent {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];      

    /* Do lots of stuff *.
    /* Periodically, I d drain and recreate the pool */
    [pool drain];
    pool = [[NSAutoreleasePool alloc] init];
    /* The rest of my stuff */

    [pool drain];
}

And doStuff: is called using detachNewThreadSelector:

ok some BIG problems i see is

1

this..

[self performSelectorOnMainThread:@selector(updatePlayerTable:) withObject:scores waitUntilDone:NO]; is passing scores which could be getting retained by something else and that would also retain all the objects it contains.

2

scores is a nsmutablearray and is explicitly defined as NOT THREAD SAFE yet you are passing it across threads.

3

those [blah JSONvalue] things should be autoreleased and that is not apple api, apple has no public JSON api for iphone. that is most likely SBJSON library which puts categories on apple classes(nsstring, nsarray, nsdictionary, etc) for convenient JSON parsing.





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签