Note that there are some explanatory texts on larger screens.

plurals
  1. PONSAlert bug with beginSheetModalForWindow
    text
    copied!<p>ABTRACT: When the method <code>beginSheetModalForWindow:modalDelegate:didEndSelector:contextInfo</code> supplies an object for <em>contextInfo</em> that is an NSArray or an NSDictionary, it will in practice become an arbitrary pointer to anywhere in memory. What is the proper solution to this?</p> <p>The following illustrates the problem:</p> <pre><code>- (IBAction)testbutton:(id)sender { NSAlert *alert = [[NSAlert alloc] init]; [alert beginSheetModalForWindow:theWindow modalDelegate:self idEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:[NSArray arrayWithObject:@"bla bla"]]; </code></pre> <p>}</p> <pre><code>- (void) alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(NSArray *)contextInfo { NSLog(@"contextInfo: %@", contextInfo); </code></pre> <p>}</p> <p>The NSLog will output anything arbitrary in memory, including a crash if the pointer is empty. However, if contextInfo is something simpler, such as an NSString, NSNumber, or even an outlet such as an NSTextField, it will work ok. Also, if I supply an instance variable that is an NSArray or an NSDictionary, then it works.</p> <p>In Apples documentation, <em>contextInfo</em> is declared as (void *), so it can be anything (even an arbitrary pointer). But since I know the class, I declare it NSArray in this example. The problem, however, does not depend on what I declare contextInfo to be.</p> <p>The problem occurs in Lion when I have Garbage Collection.</p> <p>In message <a href="https://stackoverflow.com/q/6855573/1268345">Using NSAlert beginSheetModalForWindow with contextInfo</a> 'noa' says "<em>If you're using garbage collection, store in an instance variable – anywhere, doesn't matter – so it isn't released at the end of the run loop. You need to stick around until the didEndSelector can act on it</em>."</p> <p>That is a good workaround, but it's weird, and pretty ugly. Aren't there any more elegant solutions?</p> <p>Is this behavior really what Apple intends? Or a clear bug? Simple objects such as NSString and NSNumber are ok, but not NSArray. If it really isn't a bug, shouldn't the limitations of contextInfo be specified, and shouldn't it be specified exactly when and how it needs to be explicitly retained in some way? Or will GC simply not guarantee anything whatsoever with this method unless <em>contextInfo</em> is an instance variable?</p>
 

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