简体   繁体   English

奇怪的内存地址导致BAD_ACCESS

[英]Strange memory address resulting in BAD_ACCESS

I'm trying to wrap my head around a strange problem in my iPad app. 我正在努力解决iPad应用程序中一个奇怪的问题。 It's a pretty simple app, only one root view controller with a little board game inside. 这是一个非常简单的应用程序,只有一个根视图控制器,里面有一个小棋盘游戏。

Sometimes, seemingly at random, the app freezes and I get a BAD_ACESS on a delegate reference I use in one of my classes. 有时,看似随机的,该应用程序冻结,并且我在一个类中使用的委托引用上得到了BAD_ACESS。 I've been solving BAD_ACCESS-problems for a long time, but this is very strange. 我已经解决BAD_ACCESS问题很长时间了,但这很奇怪。 The object the delegate is referring to is the root view controller, and that should never b released. 委托所引用的对象是根视图控制器,并且永远不要释放它。 I put a log line in the -(void)dealloc method and that never occurs. 我在-(void)dealloc方法中放置了一条日志行,但从未发生。 I even tried to over retain the object but it still disappears. 我什至尝试过保留对象,但它仍然消失了。

Even if I run the app in the profiler with NSZombie detection on, the profiler just stops when this happens. 即使在启用了NSZombie检测的情况下在探查器中运行该应用程序,探查器也只会在发生这种情况时停止。 Doesn't show any results whatsoever. 不会显示任何结果。

Another strange thing I noticed was the memory address. 我注意到的另一个奇怪的事情是内存地址。 If I log it like NSLog(@"%p", delegate); 如果我像NSLog(@“%p”,委托)一样记录它; i get "0x1" as a result. 我得到“ 0x1”的结果。 A nil pointer is 0x0 so testing for if(delegate) does return true even though the object has vanished. nil指针为0x0,因此即使对象已消失,测试if(delegate)的确会返回true。 And even if the object itself was deallocated, the memory address would still be intact? 即使对象本身已被释放,内存地址是否仍将完好无损?

The problem only occurs after some use, between like 15 and 45 sec. 仅在使用15到45秒后,才会出现此问题。

Doed someone know how to tackle this problem? 有人知道如何解决这个问题吗? I'd be greatly thankful. 我将非常感谢。

This is how the delegate is assigned. 这就是委托的分配方式。 The _delegate is the root view controller which is always active. _delegate是始终处于活动状态的根视图控制器。

-(id)initWithDelegate:(NSObject <TheGameDelegate>*)_delegate level:(int)_level;
{
    self = [super init];
    if(self != nil)
    {
        delegate = [_delegate retain];
        ...

Here is where it crashes: 这是它崩溃的地方:

-(void)countdown:(NSTimer*)timer;
{
    time -= 1;
    if(delegate) // this is always true
    {
        NSLog(@"%p", delegate); // this prints a normal memory address until right before crash when it prints "0x1"
        [delegate theGameTick:self]; // accessing deleagte gives BAD_ACCESS
    }
    ...

Thanks in advance! 提前致谢!

imo, your delegate implementation is weird.Retaining delegates is not the good idea. imo,您的委托实现很奇怪。保留委托不是一个好主意。

Try adding to the header: @property (nonatomic, assign) id delegate and then the method would look like 尝试添加到标题中: @property (nonatomic, assign) id delegate ,然后该方法将如下所示
-(id)initWithDelegate:(id)_delegate level:(int)_level; { self = [super init]; if(self != nil) { self.delegate = _delegate; ...}

as well as in further methods you reference to self.delegate. 以及您引用self.delegate的其他方法。
(you can add the required protocol as well, i skipped it for clearer code) (您也可以添加所需的协议,我将其跳过以获取更清晰的代码)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM