How to find a useless link that prevents dealloc in objective-c / Xcode?

I have a problem in the iOS application, which after some time is not canceled, as it should be. I suspect that this is because there is still a link to it. I am using ARC.

I want to find out where this link is created. Then I can tell where it should be NULLED or if it should be weak.

What I present as a possible solution:

If I could set a breakpoint for every place where the link count, so how to keep the count changed, then I will quickly find the problem. I just don't know how to set such a breakpoint. Perhaps in pre-ARC times this could be done by setting breakpoints inside retainand release, but I have no idea how to do this with ARC.

A very simplified code example:

I did this in one of my classes, and I know where:

// ShouldBeDeallocated.m

- (void) dealloc {
    NSLog(@"%s", __FUNCTION__); // this never shows up in output
}

And I suspect that some time ago I wrote code like this, but I cannot find where it was:

// UnknownSuspect.m

@interface UnknownSuspect ()

@property (strong, readwrite) id referenceWhichIsNeverNeeded;

@end

- (void) someMethod:(ShouldBeDeallocated*)ref {
    self.referenceWhichIsNeverNeeded = ref;
    // The object pointed to by referenceWhichIsNeverNeeded will
    // not be dealloc'ed unless the property gets overwritten.
}
+4
source share
1 answer

You can use tools to allocate profile memory. Then you can see where in your code it selects it and changes the save count. The tools also help keep track of where you have memory leaks in your code, which is still possible even when using ARC.

+1
source

Source: https://habr.com/ru/post/1529005/


All Articles