Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7503661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:18:30+00:00 2026-05-29T21:18:30+00:00

I have been struggling with this piece of code for a while and I

  • 0

I have been struggling with this piece of code for a while and I just don’t know why it happens that when running in the device … the app crashes with an EXC_BAD_ACCESS Error but when running in the simulator it runs fine.

The scenario: A subclass of NSOperation that makes an async connection with NSURLConnection and gets custom data. When finished, it calls the block with the downloaded data.

Here is the .h file:

@interface FileDownloader : NSOperation <NSURLConnectionDataDelegate>

typedef void (^CompletionBlockForFile)(NSData *);

- (id)initWithCompletionBlock:(CompletionBlockForFile)block;

@end

and the .m file:

@interface FileDownloader ()

@property (strong, nonatomic) CompletionBlockForFile completionBlock;

@property (strong, nonatomic) NSMutableData *downloadedData;

- (void)downloadFileWithCompletionBlock:(CompletionBlockForFile)block;

@end


@implementation FileDownloader

@synthesize downloadedData = _downloadedData;
@synthesize completionBlock = _completionBlock;

- (id)initWithCompletionBlock:(CompletionBlockForFile)block
{
    self = [super init];
    if (self) {
        _completionBlock = block;
    }
    return self;
}

- (void)main
{
    if (self.isCancelled) return;
    if (_completionBlock) {
        [self downloadFileWithCompletionBlock:_completionBlock];
    }
}

- (void)downloadFileWithCompletionBlock:(CompletionBlockForFile)block
{   
    NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    dispatch_async(dispatch_get_main_queue(), ^{
        NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
        [connection start];
    });
}

... delegate methods of NSURLConnection

@end

And Finally, the method that adds the operation object to the queue at the MainViewController Class:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    FileDownloader *fileDownloader = [[FileDownloader alloc] initWithCompletionBlock:^(NSData *data){      // <----- HERE IS THE EXC_BAD_ACCES ERROR JUST WHEN RUNNING IN THE DEVICE !!! :S
        NSLog(@"%@", data);
    }];

    [queue addOperation:fileDownloader];
}

Can anybody explain me what am I doing wrong? And is it correct to put strong for the block var in the property? Why not assign? Or weak ?

Thanks in advance 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T21:18:32+00:00Added an answer on May 29, 2026 at 9:18 pm

    Blocks should be copied and not retained.
    Change

    @property (**strong**, nonatomic) CompletionBlockForFile completionBlock;
    

    to

    @property (**copy**, nonatomic) CompletionBlockForFile completionBlock;
    

    and it should work

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been struggling with this for a while and just can't work it
I have been struggling in fixing this code, wonder what is wrong at all...
I have been struggling with this for a while and could really use some
I have been struggling with this a while now. I implemented a basic IHttpHandler
I have been struggling with this for a while now. I have a Master
I have been struggling with this for a while now so I thought I
I have been struggling with this issue for 3 hours. I know I can
I have been struggling with this for a while and can't find the answer
I've been struggling with a piece of C# code and although I have found
I have been struggling with this for a while, so I hope some of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.