9

I can't figure out how to allocate and open a new NSWindow without nib.

NSRect frame = NSMakeRect(100, 100, 200, 200);
NSUInteger styleMask =    NSBorderlessWindowMask;
NSRect rect = [NSWindow contentRectForFrameRect:frame styleMask:styleMask];
NSWindow * window =  [[NSWindow alloc] initWithContentRect:rect styleMask:styleMask backing: NSBackingStoreBuffered    defer:false];
[window setBackgroundColor:[NSColor blueColor]];
[window makeKeyAndOrderFront: window];

The code above is taken from this thread How do I create a Cocoa window programmatically?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
kilianc
  • 7,397
  • 3
  • 26
  • 37
  • 1
    Why would you choose to impliment the answer with the least number of upvotes? – user160917 Aug 26 '11 at 00:00
  • i tried all the ways, this answer seems correct imho :( – kilianc Aug 26 '11 at 00:16
  • 1
    What's the connection to ARC? – Terry Wilcox Aug 26 '11 at 00:18
  • 1
    I hope some of you can reply to this, I'am using ARC, but I'am pretty sure this is not the problem. – kilianc Aug 26 '11 at 00:23
  • A side note: You may want to change NSBorderlessWindowMask unless you want the window without any titlebar or controls. Instead, use something like NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask – iii Aug 26 '11 at 00:29
  • 5
    Do you hold on to the pointer to that window? If I'm not mistaken, if you don't keep `window` around as an instance variable, it will be deallocated in ARC as soon as you stop referring to it. – Brad Larson Aug 26 '11 at 00:32
  • 2
    @Brad Larson: You are not mistaken. Automatic Reference Counting means that the object in the `window` local variable will be sent a release message when the variable goes out of scope—*automatically*. The window needs to be stored in an instance variable to keep it alive (as long as the object that owns it remains alive and doesn't let go of it). – Peter Hosey Aug 26 '11 at 06:32
  • 1
    @Terry Wilcox: The code in the question would work (but leak) under manual reference counting. It's because kilianc is using ARC that the window is released at the end of this method's scope. Without ARC, it would work (but leak) because the window would never be released. – Peter Hosey Aug 26 '11 at 06:35

1 Answers1

17

If you're using ARC, then unless you have a strong reference to the window, it will be released immediately after the last statement that references it.

ARC changes the way you need to think about objects, from a retain/release model to an ownership model. If nothing owns your window variable, it will go away.

There are several ways to take ownership of the window. You can set the window as either an instance variable or as a property in your class using the strong keyword, or you can use the __strong qualifier when you declare the variable in your code.

There is a lot more information about ARC at the LLVM compiler site.

Rob Keniger
  • 45,830
  • 6
  • 101
  • 134
  • 1
    __strong qualifier didn't solve the problem, instance variable did, ty – kilianc Aug 26 '11 at 00:59
  • 2
    the reason the `__strong` didn't work is because the code (likely a method) went out of scope, and with it all those local variables as well – bshirley Jan 30 '13 at 20:06