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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:58:56+00:00 2026-06-09T11:58:56+00:00

I have this strange crash relating to ARC auto-inserting objc_retains in my code. I

  • 0

I have this strange crash relating to ARC auto-inserting objc_retains in my code.

I have the following two classes:

@interface MenuItem : NSObject
@property (weak, nonatomic) id target;
@property (unsafe_unretained, nonatomic) SEL action;
@property (strong, nonatomic) id object;
- (instancetype)initWIthTarget:(id)target action:(SEL)action withObject:(id)object;
- (void)performAction;
@end

@implementation MenuItem 
- (void)performAction
{
    if (self.target && self.action)
    {
      if (self.object)
      {
        [self.target performSelector:self.action withObject:self.object];
      }
      else
      {
        [self.target performSelector:self.action];
      }
    }
}
@end

@interface Widget : NSObject
- (void)someMethod:(id)sender;
@end

At some point I instantiate a MenuItem as such:

MenuItem *item = [MenuItem alloc] initWithTarget:widget action:@selector(someMethod:) object:nil];

Then elsewhere I invoke performAction on the menu item:

 [item performAction];

In the implementation of someMethod I get a crash:

@implementation Widget
- (void)someMethod:(id)sender
{
  // EXEC_BAD_ACCESS crash in objc_retain
}
@end

Why is this happening?

  • 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-09T11:58:57+00:00Added an answer on June 9, 2026 at 11:58 am

    The reason for the crash was because I was using the wrong performSelector.

    NSObject defines multiple versions of performSelector. The one I was invoking was:

    - (id)performSelector:(SEL)aSelector;
    

    However the method I was invoking took an id parameter. Eg:

    - (void)someMethod:(id)sender;
    

    Now ARC being the nice safe memory management system that it is tries to ensure that parameters are properly retained during the execution of a method. So even though my someMethod: was empty ARC was producing code that looked like this:

    - (void)someMethod:(id)sender 
    {
        objc_retain(sender);
        objc_release(sender);
    }
    

    The problem with this however was that I was invoking performSelector: and not supplying a value for the sender parameter. So sender was pointing at random junk on the stack. Therefore when objc_retain() was invoked the app crashed.

    If I change:

    MenuItem *item = [[MenuItem alloc] initWithTarget:widget 
                                              action:@selector(someMethod:) 
                                              object:nil];
    

    to

    MenuItem *item = [[MenuItem alloc] initWithTarget:widget 
                                              action:@selector(someMethod) 
                                              object:nil];
    

    and

    - (void)someMethod:(id)sender;
    

    to

    - (void)someMethod;
    

    Then the crash goes away.

    Similarly I can also change

    [self.target performSelector:self.action];
    

    to

    [self.target performSelector:self.action withObject:nil];
    

    if I want to follow the ‘standard’ form of target-action methods that take a single parameter. The benefit of the second form of performSelector is that if I’m invoking a method that doesn’t take a parameter it will still work fine.

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

Sidebar

Related Questions

I have this strange problem in my unit tests. See the following code _pos
I have a problem with a strange crash on iPad 5.0. This crash only
I have this strange behavior in my JSP page: I have two Integer variables
I have realy strange behavior. I have this code (I remove unrelated code). $(function()
I have this strange problem. I am retrieving twitters and it works on the
I have this strange error, When I call $element_attrs = $element -> attributes(); I
i have this strange problem with the PHP function CTYPE_ALNUM if i do: PHP:
Can anyone tell me why I always have this strange output after running this
Starting from Android 3.2 I have this strange problem. It's very easy to reproduce:
So i have this rather strange issue, i am trying to line up some

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.