如何在Xcode Instruments中演示内存泄漏和僵尸对象? [英] How to demonstrate memory leak and zombie objects in Xcode Instruments?

查看:124
本文介绍了如何在Xcode Instruments中演示内存泄漏和僵尸对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个类的Xcode Instruments中演示内存泄漏和僵尸对象.我发现故意触发很难,尤其是在ARC打开的情况下.你知道我怎么创建它们吗?

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?

推荐答案

对于泄漏:

创建两个类A和B.A应该具有一个强烈引用B实例的@property.B应该具有一个强烈引用A实例的@property.

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;

那会造成泄漏.

创建类型为assign的@属性(或类型为__unsafe_unretained的变量.然后:

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.

这篇关于如何在Xcode Instruments中演示内存泄漏和僵尸对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆