1

I am developing a small and simple status menu application.
There is a menu and when the user clicks on it, a HUD window (NSPanel) should appear. This is how I show the panel:

[hudWindow makeKeyAndOrderFront: self]; 

This is how I dismiss the window:

[hudWindow orderOut: nil];

So that's the events chain:

  1. When the app starts I dismiss the window;
  2. Then the user (that's me :-)) clicks on the menu item and makes the panel appear;
  3. Then I click on the x and close the panel;
  4. Then I click again on the menu item and the window doesn't appear again.

It doesn't appear again probably because it gets deallocated, and I have put a weak storage, otherwise with __unsafe_unretained it would crash at the second launch.
So I'm guessing if there's a way to avoid deallocating the window when the user clicks on it.
I have tried with a strong storage but this case at the second launch I'm unable to close it again and that's a problem.
There are many menu status applications that are able to display a window without that the user can "kill" it, I would make something of similar.

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
  • 3
    Have you unchecked the "Release When Closed" checkbox in IB (or done the equivalent in code)? That box is checked by default for panels. – rdelmar Dec 18 '12 at 00:10
  • That did it, there's still to clarify why with strong it doesn't work. – Ramy Al Zuhouri Dec 18 '12 at 00:16
  • Probably because the releasedWhenClosed setting overrides that. Having a strong pointer just means that the retain count will be at least one, but that doesn't prevent the system from explicitly sending a release message to the panel. – rdelmar Dec 18 '12 at 00:21
  • I presume that releasedWhenClosed is ignored under ARC. Are you using ARC? – paulmelnikow Dec 18 '12 at 00:22
  • @rdelmar: You should post that as an answer. – Peter Hosey Dec 18 '12 at 05:21

2 Answers2

2

You should uncheck the "Release When Closed" checkbox in IB (or done the equivalent in code). That box is checked by default for panels.

Using a strong pointer probably doesn't work because the releasedWhenClosed setting overrides that. Having a strong pointer just means that the retain count will be at least one, but that doesn't prevent the system from explicitly sending a release message to the panel.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • 2
    Moreover, the “Release When Closed” setting is not the same as an ownership. What that setting does is releases *your* ownership _for you_. If you use ARC, there's no way to express that this will happen—ARC will always release your ownership, ignorant of what the NSWindow may or may not intend to do—so you should turn “Release When Closed” off. (On the flip side, if you are loading the window directly from a nib, you should have it *on* to cancel out the implicit retain. See the Resources Programming Guide for the gory details.) – Peter Hosey Dec 18 '12 at 10:23
0

Can't you just change your property from weak to strong?

paulmelnikow
  • 16,895
  • 8
  • 63
  • 114
  • All I see is "I have put a weak storage, otherwise with __unsafe_unretained it would crash at the second launch" – is that what you mean? It doesn't explain why you can't change it to strong. – paulmelnikow Dec 18 '12 at 00:16
  • 4th line from top: "I have tried with a strong storage but this case at the second launch I'm unable to close it again and that's a problem." I'm unable to close the panel if I do so.Maybe the window is directly deallocated and not released? – Ramy Al Zuhouri Dec 18 '12 at 00:18
  • If you're using ARC, you'll need to leave the property as strong to avoid having the window released. You could try calling `-performClose:` on the window, instead of `-orderOut:`. – paulmelnikow Dec 18 '12 at 00:23
  • Even if I do so there's the same problem. – Ramy Al Zuhouri Dec 18 '12 at 00:24
  • PS: It seems like dead, I can move it around, but I can't resize it and neither close it. – Ramy Al Zuhouri Dec 18 '12 at 00:26
  • 1
    I do something similar with a status item, and `-makeKeyAndOrderFront:` followed by `-orderOut:` works for me. The only other thing I'm doing which appears relevant is `[NSApp activateIgnoringOtherApps:NO]` before `-makeKeyAndOrderFront:`. Sorry but I don't know why your close button isn't doing as you expect. – paulmelnikow Dec 18 '12 at 00:35