I would like to demonstrate memory leak and zombie object in Xcode Instruments on a class. I've found it pretty hard to trigger on purpose, especially with ARC turned on. Do you know how could I create them?
Asked
Active
Viewed 758 times
1

Gabriel.Massana
- 8,165
- 6
- 62
- 81

itarato
- 795
- 1
- 8
- 24
-
Are you asking how to use instruments or how to cause a leak? – Ramy Al Zuhouri Dec 09 '12 at 21:29
-
How to cause a leak - so I can demonstrate the benefits of Instruments. – itarato Dec 09 '12 at 22:21
2 Answers
3
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.
0
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;

user4951
- 32,206
- 53
- 172
- 282