Why retain count of self increases inside block? -
__block typeof(self) selfpointer = self; [studentclass callcomputersciencestudent:dept completionblock:^(department *dept) { [selfpointer getentry:dept]; } errorblock:^(department *dept) { [selfpointer deleteentry:dept]; }];
here self has retain count of 2. , selfpointer readonly. changes required make selfpointer read-write instead of readonly.
blocks capture variables used in context. if want avoid capturing strong reference, may following: __weak typeof(self) weakself = self;
[studentclass callcomputersciencestudent:dept completionblock:^(department *dept) { [weakself getentry:dept]; } errorblock:^(department *dept) { [weakself deleteentry:dept]; }];
this ensures weak reference of self
used within block.
additionally, @mttrb pointed out in comment, using retaincount
absolutely unreliable. in order avoid memory issues blocks, suggest read great article.
Comments
Post a Comment