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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:49:25+00:00 2026-06-10T16:49:25+00:00

I’m having trouble with a broken pointer that’s pointing to garbage after an object

  • 0

I’m having trouble with a broken pointer that’s pointing to garbage after an object has been released. objectA is the delegate for a callback from another object, objectB.
objectA is being allocated and released quite often (in my application its a menu UI object). Every time objectA is being initialised it initialises objectB and begins an asynchronous operation and then calls back to objectA via the id delegate property.
How can I stop my pointer: id delegate from breaking ?

ObjA.m
-(void)dealloc{
     [super dealloc];
} 

+(id)init{
     ObjA *objectA = [[ObjA alloc]init];
     return objectA;
}
-(id)init{
     if (self == [super init]){
         ObjB *objectB = [ObjB initialiseWithDelegate:self];

     }
     return self;
}    
-(void)callback:(id)arg{
     //This callback is called from an asynchronous routine from  objectB 

}
-(void)terminate{
  //This is called when I want to remove/release objectA
  //Other logical termination code here
  [self release];
}


ObjB.m
@synthesize delegate;

+(id)initialiseWithDelegate:(id)delegate{
     ObjB *objectB = [[ObjB alloc]init];
     [objectB setDelegate:delegate];
     return objectB;
}
-(id)init{
     if (self == [super init]){
         [self beginAsynchronousOperation];

     }
     return self;
}
-(void)beginAsynchronousOperation{
     //Do asynchronous stuff here
}
-(void)finishedAsynchronousOperation{
     //Called when asynch operation is complete

     if (self.delegate) [self.delegate callback:someargument]; //EXC error. Pointer broke

}
  • 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-10T16:49:26+00:00Added an answer on June 10, 2026 at 4:49 pm

    The short answer here is that you nil out objectB’s delegate property when you dealloc objectA. Because delegates are assigned, and not retained (explicitly to prevent retain cycles), as you have seen, the delegate reference can be left hanging when the “owning” object goes away. Typically objectA will be holding a retained reference to objectB, and during objectA’s dealloc, it will first set objectB’s delegate to nil, and then release objectB. This will prevent the crash. Of course this assumes (as is typical) that you don’t need to do anything with that async completion. It also assumes that objectB can safely be released by objectA when it is in the middle of an async operation. This is usually true of (say) animations, but if you’re building your own tasks, you might need to be careful of the lifetimes here.

    Some notes on this code snippet that might be helpful:

    1. Your objectA isn’t actually holding a reference to objectB once it’s created. This means you can’t nil out the delegate. You should keep the reference to objectB when it’s created so you can do this.

    2. You are leaking objectB. ObjectA creates it (alloc, init), but then drops the reference, so even when it’s done, no one seems to be responsible for releasing it. Again, holding it so you can release it will fix this too.

    3. Your -terminate on objectA is an anti-pattern– an object should never (with only one exception: a failure inside init) be calling -release on self. An object should be released by its owner, which is whoever originally created it.

    4. Your pattern of if (self == [super init]) is normally written with one equals sign, meaning that you’re both assigning self to the result as well as checking it for nil. This is a Cocoa historical oddity, and probably makes no difference here, but worth pointing out.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.