0

I am getting an expression result unused, the cause of this is pretty obvious but I would like to avoid this warning in the cleanest way possible:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [_window setReleasedWhenClosed:TRUE];
    [_window close];
    [[HotkeyHandler alloc] init:self];
}

HotkeyHandler is another class that basically listens for hotkeys. I just need it to be initialized and that's all it needs to do. I don't use any of its methods since these will be triggered by system notifications. Any ideas on circumventing this warning?

  • I deleted by answer `(void)[[HotkeyHandler alloc] init:self];` And if you only want to suppress the warning, then you can use a macro, flag that would not show any warning. – Anoop Vaidya Apr 03 '13 at 16:28

1 Answers1

1

The problem is that the instance you created will be dealloc'ed once you leave that method, so it won't be able to listen the notifications. Why don't you make that class a singleton?

Marcelo
  • 9,916
  • 3
  • 43
  • 52
  • The question was about avoiding a warning. – Kristof Van Landschoot Apr 03 '13 at 14:33
  • But the warning isn't the problem. It's a symptom that's something is wrong. – Marcelo Apr 03 '13 at 14:36
  • This raises another question. My application is working fine (i.e. the hotkeyhandler is listening to the hotkeys without a problem). If it is dealloc'ed since there are no more references to this instance, how come it keeps on existing in memory space? – Alfie Subiotto Apr 03 '13 at 14:38
  • Maybe it's kept on memory for some other reason. Whatever reason it is, it's the wrong reason. – Marcelo Apr 03 '13 at 14:40
  • +1 to balance. This is the right answer, you just refuse to accept it. Your warning is a symptom of a larger problem: that you assume that anything you don't retain in local scope is retained for you. – CodaFi Apr 03 '13 at 14:52
  • I'm not refusing to accept it, I understand the answer and it is probably what I am going to do. I'm not making any assumptions, the HotkeyHandler instance is retained since hotkeys are handled and responded to when the application is running. I am now simply curious as to why this is the case with ARC in the picture. – Alfie Subiotto Apr 03 '13 at 14:56
  • Does `HotKeyHandler` have any blocks in it? or any `strong` references to `self` – Paul.s Apr 03 '13 at 14:58
  • It may be retained because of something you do inside HotkeyHandler class. – Marcelo Apr 03 '13 at 14:58
  • Oh, I think I got it: I have a global reference to self in HotkeyHandler since I have to call some of its obj-c methods in some c functions inside the class. That must be why it is retained. On another note: since this is my first time using the singleton pattern I would just like to check. I simply add a class method that returns an instance of the class like this right? http://stackoverflow.com/questions/145154/what-should-my-objective-c-singleton-look-like – Alfie Subiotto Apr 03 '13 at 15:03
  • Prefer the dispatch_once pattern to any static atomic singleton. – CodaFi Apr 03 '13 at 15:06
  • Direct link to dispatch_once pattern answer: http://stackoverflow.com/a/145395/1777634 – Marcelo Apr 03 '13 at 15:08
  • OK, great, I will use the dispatch_once pattern for safety. Thank you to all that have helped. – Alfie Subiotto Apr 03 '13 at 15:09
  • I don't see what shall be wrong with the code he posted initially. [[HotkeyHandler alloc] init:self]; The alloc increases the retain count and the object remains retained. – decades Jun 27 '13 at 12:30
  • It increases, but under ARC it'll be decreased once you leave the method, thus dealloc'ing the object. – Marcelo Jun 27 '13 at 12:36