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 8006019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:22:26+00:00 2026-06-04T17:22:26+00:00

I had a problem with ARC and blocks but have solved the problem. Unfortunately

  • 0

I had a problem with ARC and blocks but have solved the problem. Unfortunately I don’t what exactly is going on and would like to learn more about the situation I had.

Originally, I had code that did this

for(__block id<Foo> object in objects) {
    foo download:someParm
         success:^{
            object.state = StateNewState; 
         }
    ];
}

This caused an imbalance in retains. A crash occurs when an object is accessed and is said to be already deallocated. I wrote a class that implemented and used the “copy” attribute to create a successBlock property which saved the block passed into the success parameter of the download function. I replaced the code with this with the following

for(id<Foo> object in objects) {
    foo download:someParm
         success:^(id<Foo> successObject){
            successObject.state = StateNewState; 
         }
    ];
}

No more deallocated object errors but I have yet to run instruments to check if I’m not leaking. Some how using __block is causing the object to be released too many times, and I can’t figure out why. I’ll continue my research as to the cause of this issue but I thought it’d be an interesting problem for the rest of you guys to think about.

I suppose it may be worth noting that the objects array was an autoreleased array that was created in the lines proceeding the code I wrote down earlier in this post. Don’t think it’d matter but I thought I’d just through that out there. The code I put in this post isn’t the exact code, because I’m using this for work and there’s a bunch of fluff in there. But there’s no other objects being created in the for loop.

When the app crashes, it runs download and then later runs the callback, I’m using ASIHttp by the way. When I attempt to download again it runs and the callback is not called, since object has been deallocated and the delegate is nil’ed. After this when object is accessed by a dictionary that contains a pointer to object we crash.

  • 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-06-04T17:22:28+00:00Added an answer on June 4, 2026 at 5:22 pm

    The Blocks Programming Topics says:

    Use of instance variables within the block will cause the object
    itself to be retained. If you wish to override this behavior for a
    particular object variable, you can mark it with the __block storage
    type modifier.

    If you are using ARC, object variables are retained and released automatically as the block is copied and later released.

    Thus, if you don’t use __block, I think you’ll find your variables are being retained for you. E.g., this seems to work for me:

    NSMutableArray *array = [[NSMutableArray alloc] init];
    
    // add two custom objects to that array
    
    [array addObject:[[MyObject alloc] initWithText:@"One" number:1]];
    [array addObject:[[MyObject alloc] initWithText:@"Two" number:2]];
    [array addObject:[[MyObject alloc] initWithText:@"Three" number:3]];
    
    // now replace the text field in each of the three objects with the word "Done"
    
    for (MyObject *object in array)
    {
        [self blockTestInvocation:^{
            NSLog(@"%s %@", __FUNCTION__, [NSDate date]);
    
            object.text = @"Done";
        }];
    }
    

    I found that the presence or absence of __block didn’t have a material impact when my blockTestInvocation invoked the passed block synchronously, but when I set it up to invoke the block asynchronously (after my array and objects would have otherwise been released), the absence of the __block ensured that the object was retained, thereby preventing the dreaded "message sent to deallocated instance" (and there were no leaks, either). But with __block, the object is not retained, and thus could be deallocated by the time the block of code tries to reference it.

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

Sidebar

Related Questions

Had a problem with the recursive conflictCheck() method. That seems fine now. I have
I had this problem some time ago and I gave up but lately it
Something I have always had problem with is structuring my web applications. When I
i'm using jquery in report section in my asp.net mvc project.But i had problem
I have an exercise in problem solving for those who like that kind of
I wrote simple class in JS witch works, but i had problem when i
_ I had problem with installing the mod_wsgi.I have found the solution as below
I had problem in my other code and huge, but I had made one
I had no problem with this in Visual Studio 2008 but it seems that
I had a problem that was partially solved. To explain it quickly : I

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.