内存泄漏在非常小的方法(objective-c/iphone)



我在iPhone应用程序中遇到了一个非常小的方法问题。

样本代码(类似于真实代码(:

+ (void) drawSomething: (UIView *) theView anImportantNumber: (NSNumber *) importantNumber {
    UIImage *tehImage = [[WebviewUtil sharedInstance] goldStarImage];
    int iCount = 0;
    double roundedNumber = round([importantNumber doubleValue]);
    for (; iCount <= (4 - roundedNumber); iCount++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((iCount * 15) + 9, 6, 14, 13)];
        [imageView setImage:tehImage];
        [imageView setOpaque:YES];
        [theView addSubview:imageView];
        [imageView release];
    }
}

在线路UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake中,仪器报告内存泄漏。有人能给我一个提示吗,错在哪里?我想,[imageView release];会做到吗?!

非常感谢,如果有人能帮我的话:(

如果您使用Instruments来检测泄漏,请考虑以下事实:泄漏将向您显示泄漏内存的分配点,而不是实际发生泄漏的点。

你的代码对我来说似乎是正确的。在将imageView添加为子视图后,你正确地发布了它。问题可能出在theView或任何其他包含它的对象上。因此,您可以在代码中查看该部分(例如,theView是否正确发布?(

你使用过imageNamed:方法吗?因为此方法会导致imageView中的内存泄漏:(

尝试将断点保持在for(;iCount<=(4-roundedNumber(;iCount++(并确保在任何一点上,四舍五入的数字都不会超出范围或具有一些不应该具有的异常值。。。

最新更新