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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:31:42+00:00 2026-05-28T21:31:42+00:00

Is it possible to pass [self anyFunction] in blocks without a __weak object from

  • 0

Is it possible to pass [self anyFunction] in blocks without a __weak object from self?

As an example this is valid code from the System Framework:

[UIView animateWithDuration:0.8 animations:^{
            //Do animationStuff
        } completion:^(BOOL finished) {
            [self anyFunction];
 }];

You can pass [self anyFunction] in the completion block without a warning. But if you write your own method with a completion block, the following warning occurs: capturing ‘self’ strongly in this block is likely to lead to a retain cycle.

A working solution is quite simple (iOS 5 + ARC). Before the block declare:

__weak MyClass *weakSelf = self;

and in the completion block you have to call:

[weakSelf anyFunction];

But, back to my Question: Why there is no need in the System Framework APIs to use a __weak object and to use self without any warnings. And how to implement a method without the need of a __weak object in the block?

Thank you for your effort.

  • 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-28T21:31:43+00:00Added an answer on May 28, 2026 at 9:31 pm

    The blocks which throw up the error are ones where you capture the objects that own the block. For example

    [object performBlock:^{
        [object performSomeAction]; // Will raise a warning
    }];
    

    or

    [self performBlock:^{
        [self doSomething];    // Will raise a warning
    }];
    

    but

    [self performBlock:^{
        [object doSomething];    // <-- No problem here
    }];   
    

    Because an object retains its blocks, and a block retains it’s objects. So in both these cases, the object which performs the block owns the block, which also owns the object. So you have a loop – a retain cycle. which means the memory is leaked.

    In the example you have given – you’re looking at a class method. You’re calling the block on a UIView class, not a UIView object. A class has no memory associated with it. And you are probably calling this function from a controller, so the self reference is being retained by the block, but there is no loop because self is not retaining the block.

    In the same way that, you may have noticed, not all objects that are used in the block need to be weakly referenced – just the ones that cause a retain cycle.

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

Sidebar

Related Questions

Is it possible to pass a function/callback from javascript to a java applet? For
Is it possible to pass command line arguments into a function from within a
Is it possible, when instantiating an object, to pass-in a class which the object
Is it possible to pass parameters from template back to the script ? In
Is it possible to pass a method as a parameter to a method? self.method2(self.method1)
Let's take for example this class, which is extending MySQLDB's connection object. class DBHandler(mysql.connections.Connection):
I would like to pass two strings from current view to userViewController when this
Possible Duplicate: Pass a PHP string to a Javascript variable (and escape newlines) I
Is it possible to pass a reference to a function to another function in
Is it possible to pass a App setting string in the web.config to a

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.