memory not being released in iOS app using ARC -
i've built simple trivia game using arc. while profiling memory usage using allocations profiling tool in xcode, see memory not freed. 1 illustration of problem, have class activeplayer object:
activeplayer.h:
@interface activeplayer : nsobject @property (nonatomic, strong) nsstring * name; @property (nonatomic) nsinteger overallscore; @property (nonatomic) nsinteger questionscore; - (id) initwithname:(nsstring *)name; @end
activeplayer.m:
#import "activeplayer.h" @interface activeplayer () @end @implementation activeplayer - (id) initwithname:(nsstring *)name { self = [self init]; if (self) { self.name = name; self.overallscore = 0; } homecoming self; } /* - (void)dealloc { self.name = nil; } */ @end
and activeplayer created in createplayer method in activegame class:
[[activeplayer alloc] initwithname:name]
i'm executing next test case: start new game (which allocates 1 activeplayer), reply 1 question, , game ends (and @ point activeplayer deallocated). can start game , repeat cycle (each cycle "game", described below). while using allocations profiling tool, expect see memory has been allocated in middle of game has been deallocated after game ends (no matter how many times play game). i've found not case:
btw: each bulleted row below describes row in objects list tab of allocations tool; site won't allow me post screenshot, hence text description. rows live; i'm viewing created , still living allocations.
while game #1 in progress, see next allocations.
category=activeplayer; size=16; responsible caller=-[activegame createplayer:] category=malloc 48 bytes; size=48; responsible caller=-[activeplayer initwithname:]after game #1 complete, see following. activeplayer object has been deallocated, 48 bytes still live.
category=malloc 48 bytes; size=48; responsible caller=-[activeplayer initwithname:]if start game #2, see next while game in progress. there 2 new allocations in add-on 1 game #1.
category=malloc 48 bytes; size=48; responsible caller=-[activeplayer initwithname:] category=activeplayer; size=16; responsible caller=-[activegame createplayer:] category=malloc 144 bytes; size=144; responsible caller=-[activeplayer initwithname:]and after game #2 complete, see following. again, activeplayer object has been deallocated, "malloc x bytes" allocations still exist.
category=malloc 48 bytes; size=48; responsible caller=-[activeplayer initwithname:] category=malloc 144 bytes; size=144; responsible caller=-[activeplayer initwithname:]after that, unusual results -- if play games #3, #4, , #5, never see in-game rows category="malloc x bytes", new row category=activeplayer, freed after game ends. first 2 "malloc" rows, shown above, go on persist. i've seen other odd behavior -- while testing yesterday using iphone 6.0 simulator, live memory left behind after games #2 , #3, not games #1, #4, , #5. while memory remains allocated, times @ occurs seem vary across device , different versions of simulator.
and questions:
is understanding right shouldn't seeing live memory phone call initwithplayer after game ends , activeplayer object has been freed? if yes, what's causing it, , how deallocate it? or not need worry @ all?notes:
these screenshots come running app on iphone 4 running ios 6.1. see similar behavior running iphone simulator 5.1, 6.0, , 6.1, , saw on iphone running ios 6.0 before upgraded. in activeplayer.m, dealloc method commented out, though i've tested while it's been uncommented , have verified it's beingness called (by system; don't straight phone call dealloc anywhere). either way, behavior same. for it's worth, nil reported leaks profiling tool. while 1 illustration results in 192 bytes of live memory believe should freed, i'm seeing many of classes, i.e. appears memory allocation grows on time, think problem.
your code listed fine. looks you're still maintaining reference original activeplayer, somewhere else in code.
as side note, pattern creating activeplayer isn't norm - class doesn't phone call alloc within init method. instead, caller should perform:
[[activeplayer alloc] initwithname:@"bob"];
and init method should work homecoming value of
[super init];
ios memory-management automatic-ref-counting
No comments:
Post a Comment