Laur Stefan Laur Stefan - 4 months ago 8
iOS Question

The method parameter is deallocated before it is used

I have the next problem inside a class methods, when effectuate a few long run process, and that is that the passed parameter seems to be deallocated.

I am calling the method from outside the class.

+ (void)downloadVideoForExercise:(Exercise *)exercise{

dispatch_queue_t serialQueue = dispatch_queue_create("com.blah.queue", DISPATCH_QUEUE_SERIAL);

dispatch_async(serialQueue, ^{

[self isMaximumDownloadCapacityReached:^(BOOL success) {
if (success) {
return;
}
}];

[self shouldDownloadVideoForExercise:exercise
andCompletionBlock:^(BOOL success) {
if (success == NO) {
return;
}
}];

[NetworkManager downloadFileFromURL:exercise.videoServerURL
andSaveLocally:YES
andCompletion:^(BOOL success) {

dispatch_async(dispatch_get_main_queue(), ^{
RLMRealm *realm = [RLMRealm defaultRealm];
if (![realm inWriteTransaction]) {
[realm beginWriteTransaction];
}
if (success) {
//save the new local link
NSString *videopath = [NetworkManager getVideoLocalURLFromServerURLForExercise:exercise.videoServerURL];
exercise.videoLocalURL = videopath;
//change flag for ExerciseVideoDownloadState
exercise.videoDownloadState = isDownloadCompleted;
}else{
//change flag for ExerciseVideoDownloadState
exercise.videoDownloadState = isNotDownloaded;
}
if ([realm inWriteTransaction]) {
[realm commitWriteTransaction];
}
});

}];

});

}


I already thought about using the passed parameter with __strong or __block
but without success.
Thank you in advance for any help provided.

Answer

The issue was generated by the library that I am using, Realm. The problem was that all the changes to the object are handles in the back by some C++ class that does some custom memory handle, so when I was calling the object it was returning not a nil value but a pointer to the C++ object that was doing the job. Went around the issue by doing so:

- (void)downloadVideoForExercise:(Exercise *)exercise{
    if (!serialQueue) {
        serialQueue = dispatch_queue_create("com.blah.queue", DISPATCH_QUEUE_SERIAL);
    }

    NSString *url = exercise.videoServerURL;

    dispatch_async(serialQueue, ^{

        [self isMaximumDownloadCapacityReached:^(BOOL success) {
            if (success) {
                return;
            }
        }];

        [self shouldDownloadVideoForExercise:exercise
                          andCompletionBlock:^(BOOL success) {
                              if (success == NO) {
                                  return;
                              }
                          }];

        [self downloadFileFromURL:url
                             andSaveLocally:YES
                              andCompletion:^(BOOL success, NSString *localURL) {

                                  dispatch_async(dispatch_get_main_queue(), ^{
                                      RLMRealm *realm = [RLMRealm defaultRealm];
                                      if (![realm inWriteTransaction]) {
                                          [realm beginWriteTransaction];
                                      }
                                      if (success) {
                                          //save the new local link
                                          NSString *videopath = [self getVideoLocalURLFromServerURLForExercise:exercise.videoServerURL];
                                          exercise.videoLocalURL = videopath;
                                          //change flag for ExerciseVideoDownloadState
                                          exercise.videoDownloadState = isDownloadCompleted;
                                      }else{
                                          //change flag for ExerciseVideoDownloadState
                                          exercise.videoDownloadState = isNotDownloaded;
                                      }
                                      if ([realm inWriteTransaction]) {
                                          [realm commitWriteTransaction];
                                      }
                                  });

                              }];

    });

}
Comments