English 中文(简体)
XCode 仪器发现内存泄漏:为什么?
原标题:XCode Instruments has found a memory leak: why?

我在做一个调试程序 我创建了一个应用程序, 但运行的仪器 我发现一个记忆泄漏 我无法理解。

如果我试图按照《文书》的建议修改守则 我的申请因为

发送到交易位置实例的信件

有人能帮我吗?

- (void) objectAtIndex:(int)index {
    SpecialObject *specialObj = [SpecialObject sharedInstance];

    id model = [self.datasource objectAtIndex:index];

    if ([model isKindOfClass:[ClassA class]]) {
        ClassA *objA = (ClassA *)model;
        specialObj.title = objA.title;

    } else if ([model isKindOfClass:[ClassB class]]) {
        ClassB *objB = (ClassB *)model;
        specialObj.title = objB.title;
    }
}

self. datasource is a NSMutableArray , specialObj. is a NSString specialObj.> 标题 是一个 NSString

它们被定义为

我的问题是 文书告诉我 这2个

ClassA *objA = (ClassA *)model;
ClassB *objB = (ClassB *)model;

是泄漏,但如果我释放了 objA objB 我的应用程序崩溃 。

谢谢你的帮助!

最佳回答

经过一些搜索,我发现了问题: XCode 通常 告诉了泄漏的位置, 但有时它给了你一个可能的位置 。

这意味着内存泄漏是在程序流中某处 流向XCode点指示的, 不一定是它所说的确切位置。

希望这将来能帮别人!

问题回答

我的猜测是,你永远不会释放数据源。假设你没有在 dealloc 方法中使用 ARC,你应该

[self setDataSource: nil];

[instanceVariableThatBacksDataSourceProperty release];

许多人做前者,但苹果公司推荐后者,这样KVO就不会在交易期间触发。

漏洞和你的例外是不同的。例外几乎肯定是因为释放了一些你不该释放的东西。

你们对目标C的记忆管理的理解 并不那么出色

首先,你应集中精力理解例外情形,这样做的第一步是查看 < a href=> > 例外追踪 。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签