简体   繁体   中英

Strange BAD_ACCESS error

This may sound a newbie question however I'm new iOS dev,

recently I've switched to ARC and have got following issue in my code. I've implemented a class with following init method

- (id)init
{
    self = [super init];
    if (self) {
        TokenManager* tokenManager = [[TokenManager alloc] initWithApikey:<my valid token>];
        mRequest = [[GeoCodingRequest alloc] initWithApikey:apiKey withOptions:nil tokenManager:tokenManager];
        mRequest.delegate = self;
    }
    return self;
}

where mRequest in interface is declared like this __strong GeoCodingRequest* mRequest;

In my other method of the same class I send a message to mRequest like this

[mRequest findObject:<some valid array> around:<some valid location> withDistance:<some valid radius>];

When the message is sent to object app crashes with following message

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString appendRequestWithToken:]: unrecognized selector sent to instance 0x8974640'

findObject: around: withDistance: - is a method from public library, so I've not access to its implementation.

Please help me to understand why this crash happens ?! I guess it is coming from ARC _ strong/ _weak qualifier usage or smth.

The message means that mRequest has been deallocated has that a string is now using the memory that had previously been allocated for mRequest .

You should run your app with Zombies enabled to understand when mRequest gets deallocated.

It looks like the tokenManager isn't retained by the request (which is a bug in the library). As it isn't retained anywhere, the tokenManager is deallocated and a new object (here an instance of NSString ) may be allocated at the same place.

You need to store the tokenManager in a strong instance variable (just as mRequest ) in order to retain it as long as the request needs it.

if you have any weak references try to use

MyObject * strongMyObject = weakMyObject;
if(strongMyObject){
   NSLog(@"This is not released");
}

It's a good practice when you have weak references. Try this.

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