Note that there are some explanatory texts on larger screens.

plurals
  1. POBad Access Error when trying to pass error through functions
    primarykey
    data
    text
    <p>I am trying to implement some Errorhandling in my class. I don't think I have a full understanding of how Dealing with Errors works, but I noticed that people advised, to declare Errors in functions as (<code>NSError *__autoreleasing *error</code>) so I did that. I have problems passing the error through functions.</p> <p>The following problem happens: breaking it down it seems like the following makes problems: (Sorry, the actual code is too long but I tried to extract hopefully the most important for the problem! I hope my scope of code contains the problem)</p> <p>Imagine you have <strong>ObjectA</strong> with the method:</p> <pre><code> -(NSString *) do1: (NSString *) withstuff error:(NSError *__autoreleasing *)error{ //... //error happens *error = [[NSError alloc] initwithDomain: domain code: blah userinfo: infodict]; return nil; } -(BOOL) do2error:(NSError *__autoreleasing *)error{ NSString *doesntmatter = [self do1: @"whatever" error: error]; if (doesntmatter == nil){ return NO; } } </code></pre> <p>Now in another class (AppDelegate Object) I call the following:</p> <pre><code> ObjectA* ob1 = [[ob1 alloc] init]; NSError *errorBoom = nil; if ([ob1 do2error:&amp;errorBoom] == NO){ NSLog(@"error: %@",errorBoom); //---&gt; bad access error } </code></pre> <p>It seem like errorBoom is not accessible anymore? Is that because of "__autoreleasing", I tried to understand what it actually means but all explanations so far where not very useful for me since I am very new in this field... I hope you can help me on this!</p> <p><strong>EDIT:</strong></p> <p>Ok, I think I tracked down the error. Now I know what causes it but I don't know why exactly. I wrote an simple example application that extracts the main origin of the "Bad Access"-Error occurrence:</p> <pre><code> - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions (NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. NSError *errtest = nil; BOOL testBool = [self messaroundwitherr:&amp;errtest]; NSLog(@"Filled NSError? : %@",errtest); //&lt;--Causes Bad Access. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; } -(BOOL) giveErrornow:(NSError *__autoreleasing *)err { NSMutableDictionary *errInfo = [[NSMutableDictionary alloc] init]; [errInfo setObject:@"I feel like giving you an error!" forKey:NSLocalizedDescriptionKey]; *err = [[NSError alloc] initWithDomain:@"nonesense" code:0 userInfo:errInfo]; return NO; } -(BOOL) messaroundwitherr:(NSError *__autoreleasing *)err{ //@autoreleasepool { --&gt; uncommenting that causes the error return [self giveErrornow: err]; //} } </code></pre> <p>So apparently activating the autoreleasepool, makes the err variable be deallocated before even being able to read it from the outer function (AppFinishLaunching...). Now why is that happening exactly? I know that "@autoreleasepool" deallocates the variables after being used. In the original context I had a while loop, that's why I did it. Here it's just for comprehension reasons. So how does @"autoreleasepool" work? What happens actually with the command (*__autorelease *).</p> <p>I guess I have to fully understand the notion of it before fixing it. Does NSError (*__autorelease *) err define a "pointer to pointer of an object that is going to be autoreleased (if yes, when) " for the ARC?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload