Avoiding this callback is sent to the freed instance

The following process crashes my application:

  • the user opens the view and the request is sent to the server
  • the request is running in the background
  • user goes to root view
  • request completed.

and the following code is executed

// MyDatasource.m  
// e.g. in connectionDidFinishLoading  
[callback loadedDataSource:self];

Meanwhile, other models / views were freed and the message sent to the freed instance.

callbackhas a type idand conforms to the protocol KalDataSourceCallbacks.

How can I avoid sending a message to the selected object?

PS: My question is similar to this question

Edit:

callback nil dealloc ( datasource). , MyDataSource , dealloc RootViewController ( , ).

:

MyDataSource RootViewController:

// RootViewController.h
@property (retain) MyDataSource *dataSource;

// RootViewController.m
@synthesize dataSource;
// ...
self.dataSource = [[[MyDataSource alloc] init] autorelease];
kal.dataSource = dataSource;

- (void)dealloc {
    [dataSource release];
    // ...
}

KalViewController . , dataSource , , .

callback:

// MyDataSource.h
@property (retain) id<KalDataSourceCallbacks> callback;

// MyDataSource.m
@synthesize callback;
// ...
- (void)presentingDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate delegate:(id<KalDataSourceCallbacks>)delegate {
    // ...
    self.callback = delegate;
}
- (void)dealloc {
[callback release];
callback = nil;
   // ...
}

. . callback = nil , dealloc .

MyDataSource , ? , , . , dataSource .

2:

callback nil, , ?

if (callback != nil)
     [callback loadedDataSource:self];

.

3:

, :

@property (retain) id<KalDataSourceCallbacks> callback;

@property (assign) id<KalDataSourceCallbacks> callback;

, , loadedDataSource ?

0
3

KalDataSource.h KalDataSource protocoll:

@protocol KalDataSource <NSObject, UITableViewDataSource>
    // ...
    - (void)destroyCallback;
@end

KalDataSource.m , :

@implementation SimpleKalDataSource
// ...
- (void)destroyCallback
{
    // do nothing
}
@end

KalViewController.m , :

- (void)dealloc
{
    // ...
    [dataSource destroyCallback];
}

MyDataSource.m

- (void)destroyCallback {
    self.callback = nil;
}

.

0

, .

+1

, , . , nil, .

0

All Articles