简体   繁体   中英

Strange memory address resulting in BAD_ACCESS

I'm trying to wrap my head around a strange problem in my iPad app. 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. I've been solving BAD_ACCESS-problems for a long time, but this is very strange. 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. 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. Doesn't show any results whatsoever.

Another strange thing I noticed was the memory address. If I log it like NSLog(@"%p", delegate); i get "0x1" as a result. A nil pointer is 0x0 so testing for if(delegate) does return true even though the object has vanished. 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.

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.

-(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.

Try adding to the header: @property (nonatomic, assign) id delegate and then the method would look like
-(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.
(you can add the required protocol as well, i skipped it for clearer code)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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