How to demonstrate memory leak and zombie objects in Xcode Instruments?

天大地大妈咪最大 提交于 2019-12-09 04:32:28
bbum

For A leak:

Create two classes, A and B. A should have an @property that strongly references an instance of B. B should have an @property that strongly references an instance of A.

A *a = [A new];
B *b = [B new];
a.b = b;
b.a = a;

That'll create a leak.

For a Zombie

Create a @property that is of type assign (or a variable of type __unsafe_unretained. Then:

A *a = [A new];
A.dangerDanger = [NSObject new];

That should create a zombie situation; a dangling pointer, more specifically.

A more elegant way to show a leak is to have a class having strong pointer to itself

A* a = [A new];
a.leakingpointer=a;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!