IPhone crash - message sent to freed instance

Ok, here is the error I get:

    -[CFRunLoopTimer invalidate]: message sent to deallocated instance 0x109b05a0 (gdb)

Here is the error code:

    if (waitingOpponentTimer) {
      [waitingOpponentTimer invalidate]; //<-- Crash/error occurs here
      waitingOpponentTimer = nil;
    }

And in another place:

    NSTimer* waitingOpponentTimer;

As well as:

    waitingOpponentTimer = [NSTimer scheduledTimerWithTimeInterval:10.0f target:self selector:@selector(waitingOpponentTimeOut)userInfo:nil repeats:NO];

There are NO other waitOpponentTimer events anywhere, only the ones I showed above. The waitOpponentTimeOut action simply sets several variables and does nothing for timers or for waiting for OpponentTimer.

I tried all of the following IF statements to prevent the invalidate statement from executing:

    if (waitingOpponentTimer) {
    if ([waitingOpponentTimer retainCount] > 0) {
    if (waitingOpponentTimer.isValid) {
    if (waitingOpponentTimer != nil) {

But in all cases, it still arrives through the IF statement, and then causes a failure using the invalidate statement.

So my question is: why / how to invalidate an error when the isValid object is not nil and keepCount is greater than zero?

Is there any other way to check this to prevent invalidation?

noob iphone Google, , , . !

EDIT: , , . , , , IF, , ( ). , , , , ?

EDIT: , . , , IF, , RETAIN, IF.

: NSTimer, ?

+2
3

ARC?

waitingOpponentTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0f target:self selector:@selector(waitingOpponentTimeOut)userInfo:nil repeats:NO] retain];
+6

isValid

, 2

 waitingOpponentTimer = [NSTimer scheduledTimerWithTimeInterval:10.0f target:self selector:@selector(waitingOpponentTimeOut)userInfo:nil repeats:NO];

 waitingOpponentTimer = [NSTimer scheduledTimerWithTimeInterval:10.0f target:self selector:@selector(waitingOpponentTimeOut)userInfo:nil repeats:NO];

    [waitingOpponentTimer invalidate]

- , , , , . isValid

if ([waitingOpponentTimer isValid]
{
    [waitingOpponentTimer invalidate]

     waitingOpponentTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0f target:self selector:@selector(waitingOpponentTimeOut)userInfo:nil repeats:NO] retain];
}else{

     waitingOpponentTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0f target:self selector:@selector(waitingOpponentTimeOut)userInfo:nil repeats:NO] retain];
}
+1

, . , , . , . , , , , , , . , , ( )

0

Source: https://habr.com/ru/post/1704080/


All Articles