0

我正在开发我的第一个 iPhone 应用程序,到目前为止它运行良好,但突然它开始崩溃并且我不断收到此错误:

2012-08-13 08:39:50.000 OGLGame[36085:10a03]-[ NSCFString setFrame:]:无法识别的选择器发送到实例 0x7368300 2012-08-13 08:39:50.031 OGLGame[36085:10a03](0 CoreFoundation
__except 0x0166203ePreprocesse + 206 1 libobjc.A.dylib
0x01c7fcd6 objc_exception_throw + 44 2 CoreFoundation
0x01663cbd -[NSObject doesNotRecognizeSelector:] + 253 3
CoreFoundation 0x015c8ed0 __ forwarding
+ 432 4 CoreFoundation 0x015c8cb2 _CF_forwarding_prep_0 + 50 5 OGLGame 0x00004673 -[EAGLView setupScore] + 355 6 OGLGame
0x0000335b - [EAGLView initGame] + 299 7 OGLGame
0x00003217 -[EAGLView initWithCoder:] + 1047 8 UIKit
0x00a48135 -[UIClassSwapper initWithCoder:] + 243 9 UIKit
0x00b47c6e UINibDecoderDecodeObjectForValue + 2276 10 UIKit
0x00b47383 -[UINibDecoder decodeObjectForKey:] + 117 11 UIKit
0x00a47cad -[UIRuntimeConnection initWithCoder:] + 187 12 UIKit
0x00b47c6e UINibDecoderDecodeObjectForValue + 2276 13 UIKit
0x00b4767b UINibDecoderDecodeObjectForValue + 753 14 UIKit
0x00b47383 -[UINibDecoder decodeObjectForKey:] + 117 15 UIKit
0x00a47105 -[UINib instantiateWithOwner:options:] + 817 16 UIKit
0x00a48eb7 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 157 17 用户界面套件
0x00825ce1 -[UIApplication _loadMainNibFileNamed:bundle:] + 58 18 UIKit 0x00825ff8 -[UIApplication _loadMainInterfaceFile] + 225 19 UIKit 0x0082517f -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 279 20 UIKit 处理事件:withNewEvent:withNewEvent 0x008 ] + 1027 21 UIKit 0x00834c38 -[UIApplication sendEvent:] + 68 22 UIKit
0x00828634 _UIApplicationHandleEvent + 8196 23 GraphicsServices
0x03af7ef5 PurpleEventCallback + 1274 24 CoreFoundation
0x01636195 CFRUNLOOP_IS_CALLING_SOUT_FUNCTION_A + 53 25 CoreFoundation 0x0159aff2 __CFRunLoopDoSource1 + 146 26 CoreFoundation 0x015998da __CFRunLoopRun + 2218 27 CoreFoundation
0x01598d84 CFRunLoopRunSpecific + 212 28 CoreFoundation
0x01598c9b CFRunLoopRunInMode + 123 29 UIKit
0x00824c65 -[UIApplication _run] + 576 30 UIKit
0x00826626 UIApplicationMain + 1163 31 OGLGame
0x000029c4 main + 116 32 OGLGame
0x00002945 开始 + 53 33 ???
0x00000001 0x0 + 1)

这是我认为引发错误的函数:

-(无效)setupScore{

scoreLabel = [NSString stringWithFormat:@"foo"];
scoreLabel.frame = CGRectMake(262, 250, 100, 40);
[scoreLabel setText: scoreString];

//normally you'll want a transparent background for your label
scoreLabel.backgroundColor = [UIColor clearColor]; 

//you can use non-standard fonts
[scoreLabel setFont:[UIFont fontWithName:@"TimesNewRoman" size: 1.0f]];

//change the label's text color
scoreLabel.textColor = [UIColor whiteColor];

//you can even create a drop shadow on your label text
/*myLabel.layer.shadowOpacity = 0.6;   
 myLabel.layer.shadowRadius = 0.0;
 myLabel.layer.shadowColor = [UIColor blackColor].CGColor;
 myLabel.layer.shadowOffset = CGSizeMake(1.0, 1.0);*/

//add it to your view
scoreLabel.transform = CGAffineTransformMakeRotation(89.53);
[self addSubview:scoreLabel];  }

-(无效)resetScore { score = 0; scoreLabel.textColor = [UIColor blackColor]; [分数标签发布];}

-(void)drawScore{
[scoreLabel setText: scoreString]; }

有谁知道如何解决这个奇怪的崩溃?

如果您需要更多代码,请告诉我,谢谢!

4

3 回答 3

1

根据您发布的堆栈,很可能在[EAGLView setupScore]其中有一个地方您正在将一个变量分配为具有 just 的东西(可能是一个NSStringalloc,而不是将其init方法的返回分配给同一个 var。

// this is WRONG!!
NSString *aString = [NSString alloc];
[aString initWithFormat:@"foo"];

// this is correct
NSString *aString = [[NSString alloc] initWithFormat:@"foo"];

// even better
NSString *aString = [NSString stringWithFormat:@"foo"];
// (class methods named in this manner (start with name of class) 
// take care of alloc and init for you)

原因是 init 方法被允许返回一个与 alloc 返回完全不同的指针,并且这个新指针是有效的,旧的不是。总是以这种方式嵌套你的 alloc/init 调用。

编辑

[scoreLabel setText: scoreString];根据您在另一个答案的评论中发布的一些信息,内部调用似乎-(void) setupScore使用了不正确保留的 NSString 对象scoreString

我的猜测是该字符串的@property 被声明为弱,否则您没有在任何地方正确初始化它。

编辑 2

您对 scoreLabel 和 scoreString 变量的处理不当。在一个地方,您将 scoreLabel 属性设置为 NSString。您可能打算这样做:

scoreString = [NSString stringWithFormat:@"foo"];

然后,您假设它scoreLabel存在,但在您的其他方法之一中,您明确释放它。这意味着您需要在此处检查它是否正确存在,并在需要时创建一个新的。

于 2012-08-10T21:23:58.820 回答
0

您的代码可能正在访问无效内存。当对象被释放并在之后使用时,这种情况经常发生。您可以打开僵尸检测来阻止这些情况。

于 2012-08-10T21:46:03.150 回答
-1

堆栈跟踪显示您正在使用自己的视图,它在初始化期间EAGLView调用方法。setupScore此处的第 340 行导致崩溃。

这种方法看起来如何?

于 2012-08-11T01:20:33.133 回答