我在自定义 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
存在。
问题来了:
- 有人能看到明显的保留, 释放问题在这里?
- 我怎样才能得到
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/