objective-c - 释放实例 | "Malloc_history cannot examine process XYZ because the process does not exist."

标签 objective-c ios memory-management dealloc malloc-history

我在自定义 UIView 中使用以下方法时遇到内存问题(是的;)我是 iOS 的新手:

头文件

....    
@property (nonatomic, retain) NSString * pressureTextLabel;
....

实现绘制一个圆圈和一个带有与触摸相关的压力的标签。每个手指触摸都会创建此 View 的一个对象:

- (void)drawRect:(CGRect)theRect{
    CGRect rect = self.bounds;
    CGRect ringRect = CGRectInset(rect, 100, 100);

    // Outer ring.
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:ringRect];
    ringRect = CGRectInset(rect, 60, 60);
    [path appendPath:[UIBezierPath bezierPathWithOvalInRect:ringRect]];
    path.usesEvenOddFillRule = YES;
    [self.color set];
    [path fill];

    //text label
    rect = CGRectMake(100, 20, 100, 100);

    //This one seems to be the troublemaker
    [pressureTextLabel drawInRect:rect withFont:[UIFont systemFontOfSize:14.0]];

}

只要 Controller 不调用以下方法来更新此特定触摸的感应压力,一切都可以正常工作。

-(void) setTouchPressureTo: (float) pressure{

    pressureTextLabel = [NSString stringWithFormat:@"%f", pressure];
    [self setNeedsDisplay];

}

我收到以下错误:

*** -[CFString drawInRect:withFont:]: message sent to deallocated instance 0x16e8c0

这让我在应用程序崩溃后调查调试控制台中的内存跟踪:shell malloc_history <PID> 0x17dfb0 .结果控制台返回:

malloc_history cannot examine process 5838 because the process does not存在。

问题来了:

  1. 有人能看到明显的保留, 释放问题在这里?
  2. 我怎样才能得到 malloc_history <PID> <Address > 工作?

感谢您的宝贵时间、重定向和回答!

基督徒

最佳答案

问题是您正在将一个自动释放的对象(您的[NSString stringWithFormat...])分配给一个ivar(pressureTextLabel)。您应该改为使用属性访问,如 self.pressureLabel = ... 中所示。

关于objective-c - 释放实例 | "Malloc_history cannot examine process XYZ because the process does not exist.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4823799/

相关文章:

ios - 解析,如何处理无效的 session ?

php - 在 PHP 脚本中管理内存的最佳实践?

ios - 使用[ self 释放]、[ self 保留]是个好方法吗?

c++ - 为什么我要重载 operator new 或 operator delete?

iphone - 在引号之间获取字符串的 NSRange?

ios - 在 Swift 中从 UIView 上的特定点开始淡入淡出动画

iphone - UIPageControl 子类重写

iphone - 通过 tableview 添加 View (UITableViewController)

ios - 在绘制之前从数据库中获取数据

objective-c - 基于文档的 OSX 应用程序 - 将打开文档的数量限制为一个