Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print strong owners of an object , Swift

There are some retain cycle in my iOS application.

For a particular viewController, stuck in a retain cycle, I have tried making all delegates weak. But when I simulate memory warning from simulator , didRecieveMemoryWarning is called , but deinit method is not called .

I want to print/see the owner of that viewController that is still holding it when didRecieveMemoryWarning is called. Is there any way I can do this.

like image 862
ila Avatar asked Dec 21 '15 13:12

ila


People also ask

What is AnyObject in Swift?

You use AnyObject when you need the flexibility of an untyped object or when you use bridged Objective-C methods and properties that return an untyped result. AnyObject can be used as the concrete type for an instance of any class, class type, or class-only protocol.

How do you print an object in Swift?

In swift we can use both print() and NSLog() functions to print something on Xcode console.

How do you get the retain count in Swift?

Keeping Track of the Retain Count. Every object in Swift – an instance of a class – has a property called retainCount. As we've discussed, only reference types, such as classes, have a retain count. A value type, such as a struct, is copied when it's passed around in your code; there's nothing to reference or retain.

What is strong reference in Swift?

In Swift, a strong reference is the default, for variables, properties, constants, passing into functions (depending on what you do with it), and for closures. With ARC, an instance is only deallocated when its retain count is zero. A strong reference increases the retain count by 1, a weak reference does not.


2 Answers

If you are on Xcode 8 you can use the Memory Graph Debugger to visually see the active memory graph for objects in your projects. You can get to the Memory Graph Debugger by using the three circle icon shown below. enter image description here

The Memory Graph Debugger was highlighted at WWDC 2016 in the following lecture, starting around 24:30.

https://developer.apple.com/videos/play/wwdc2016/410/

like image 151
weber Avatar answered Oct 22 '22 08:10

weber


No there is not a way to print the owners of an object as you describe, at least not exactly. iOS does not use garbage collection, it uses ARC (Automatic Reference Counting.) The system doesn't track owning references in ARC. Instead, each time you add an owning reference to an object the system increases a retain count, and every time you clear an owning reference the system decrements that retain count.

What you can do, though, is run your program with the memory instrument. (There is a debugging application called "Instruments", you pick a debugging template called an "instrument" that you use within the Instruments program.) With the memory instrument you can follow the life-cycle of an object, as well as tracking the number of active objects in your app's heap.. When you select an object in the list of currently active object you can see where in your program it was allocated and where the code that creates strong references comes from. Explaining how to use Instruments is beyond the scope of an SO post however. There are various tutorials and WWDC session videos on the subject. I suggest doing some searching.

As to your question, forcing a low memory warning would not cause an active view controller (one that is on-screen) to be released and it's deinit method to be called. The system maintains a strong reference to the view controller.

Also, you should make delegate references weak by default. Having a delegate reference be a strong reference is VERY unusual. I've only seen it once or twice, for specific reasons.

like image 6
Duncan C Avatar answered Oct 22 '22 07:10

Duncan C