# LeakedObjectDetector hits before systemRequestedQuit is called

**URL:** <https://forum.juce.com/t/leakedobjectdetector-hits-before-systemrequestedquit-is-called/14228>\
**Category:** General JUCE discussion\
**Created:** [January 29, 2015, 3:02pm UTC](https://forum.juce.com/t/leakedobjectdetector-hits-before-systemrequestedquit-is-called/14228 "2015-01-29T15:02:45Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanKolle](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dankolle/32/40_2.png) [@DanKolle](https://forum.juce.com/u/DanKolle)\
**Post date:** [January 29, 2015, 3:02pm UTC](https://forum.juce.com/t/leakedobjectdetector-hits-before-systemrequestedquit-is-called/14228/1 "2015-01-29T15:02:45Z")

</div>

Hello,

&nbsp;

when I quit my application using Cmd+Q on Mac OS&nbsp;X, the&nbsp;LeakedObjectDetector hits from within&nbsp;MessageManager::runDispatchLoop. The method&nbsp;systemRequestedQuit of my application is never called which would clean up the remaining objects.

I was wondering if it is correct that using Cmd+Q does not trigger&nbsp;systemRequestedQuit().

&nbsp;

Thanks

Dan

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [January 29, 2015, 5:58pm UTC](https://forum.juce.com/t/leakedobjectdetector-hits-before-systemrequestedquit-is-called/14228/2 "2015-01-29T17:58:46Z")

</div>

Tried this myself and cmd-Q certainly does call&nbsp;systemRequestedQuit()

But, you shouldn't be doing any clean up in there! Your cleanup should only go in the&nbsp;shutdown() method.

---

<div class="post-metadata">

**Author:** ![DanKolle](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dankolle/32/40_2.png) [@DanKolle](https://forum.juce.com/u/DanKolle)\
**Post date:** [January 29, 2015, 7:34pm UTC](https://forum.juce.com/t/leakedobjectdetector-hits-before-systemrequestedquit-is-called/14228/3 "2015-01-29T19:34:00Z")

</div>

For me the leak detection hits before shutdown() or&nbsp;systemRequestedQuit() are called.

When I remove the leak detection assertfalse just to see if shutdown and&nbsp;systemRequestedQuit are called at all and they are not.

&nbsp;

Are those called after the window has been destroyed?

I wonder when the application closed through Cmd+Q where the currently open windows should be destroyed.

&nbsp;

I will cross-check with Windows just in case. Maybe the application instance is cleared for some reason.

---

<div class="post-metadata">

**Author:** ![DanKolle](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dankolle/32/40_2.png) [@DanKolle](https://forum.juce.com/u/DanKolle)\
**Post date:** [February 4, 2015, 12:53am UTC](https://forum.juce.com/t/leakedobjectdetector-hits-before-systemrequestedquit-is-called/14228/4 "2015-02-04T00:53:00Z")

</div>

Hey Jules,

&nbsp;

I cross checked my issue with the JUCE Demo. It is as you said, but sadly that does not change my issue.

In my application not even&nbsp;JUCEApplicationBase::appWillTerminateByForce() is called, even when I put a hard crash into it, it does not trigger so it can also not be a debugger issue.

I use custom code to create my application object but of course stick to the&nbsp;JUCEApplication class for it.

&nbsp;

Do you have a suggestion why the event handler does not seem to be in place?

Also when I register application commands with shortcuts they do not work, maybe that is related.

I attached my callstack and the one from the demo, maybe that gives someone an idea what could be wrong.

https://www.dropbox.com/s/tgvul6jj1kfgxeu/callstacks.jpg?dl=0

&nbsp;

Thanks

Dan

---

<div class="post-metadata">

**Author:** ![DanKolle](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dankolle/32/40_2.png) [@DanKolle](https://forum.juce.com/u/DanKolle)\
**Post date:** [February 4, 2015, 1:33am UTC](https://forum.juce.com/t/leakedobjectdetector-hits-before-systemrequestedquit-is-called/14228/5 "2015-02-04T01:33:00Z")

</div>

Okay I think I get an idea what is wrong.

&nbsp;

There are certain function calls that rely on the application object to exist which triggers the MessageManager to be initialised.

I found&nbsp;ScopedJuceInitialiser\_GUI and&nbsp;LookAndFeel::setDefaultLookAndFeel.

I wonder what the correct order is for those. It seems like the application object should be the first one to be created.

Also as I create my application instance myself, I overloaded&nbsp;JUCEApplicationBase::isStandaloneApp() to always return true.

Does this have other implications beyond what I see right now?

&nbsp;

Thanks  
Dan

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [February 4, 2015, 10:18am UTC](https://forum.juce.com/t/leakedobjectdetector-hits-before-systemrequestedquit-is-called/14228/6 "2015-02-04T10:18:35Z")

</div>

Ah, if you're going off-piste and doing the&nbsp;startup process yourself, then all bets are off. But yes, as long as you use those&nbsp;initialiser functions, that should be most of what you need.

---

<div class="post-metadata">

**Author:** ![DanKolle](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dankolle/32/40_2.png) [@DanKolle](https://forum.juce.com/u/DanKolle)\
**Post date:** [February 4, 2015, 9:34pm UTC](https://forum.juce.com/t/leakedobjectdetector-hits-before-systemrequestedquit-is-called/14228/7 "2015-02-04T21:34:00Z")

</div>

I modified my code so it sticks to the original initialisation code but that did not help.

AppDelegate is correctly created and the following lines are correctly executed:

```
[NSApp setDelegate: delegate];

[[NSDistributedNotificationCenter defaultCenter]
     addObserver: delegate
        selector: @selector (broadcastMessageCallback:)
            name: getBroadcastEventName()
          object: nil];
```

Still none of the oberservers is called, not even&nbsp;applicationWillFinishLaunching.

Could it be that there is an problem with the return value of&nbsp;getBroadcastEventName?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [February 5, 2015, 9:40am UTC](https://forum.juce.com/t/leakedobjectdetector-hits-before-systemrequestedquit-is-called/14228/8 "2015-02-05T09:40:22Z")

</div>

The broadcaster stuff is unrelated, it's just for interprocess messages.

---

<div class="post-metadata">

**Author:** ![DanKolle](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dankolle/32/40_2.png) [@DanKolle](https://forum.juce.com/u/DanKolle)\
**Post date:** [February 5, 2015, 10:28am UTC](https://forum.juce.com/t/leakedobjectdetector-hits-before-systemrequestedquit-is-called/14228/9 "2015-02-05T10:28:58Z")

</div>

The mainmenu tracking works. So it is only the observers for&nbsp;NSDistributedNotificationCenter that do not work.

Is there a deeper meaning behind&nbsp;getBroadcastEventName()? Because&nbsp;broadcastMessageCallback is never called.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [February 5, 2015, 10:52am UTC](https://forum.juce.com/t/leakedobjectdetector-hits-before-systemrequestedquit-is-called/14228/10 "2015-02-05T10:52:46Z")

</div>

It's irrelevant to what you're trying to do.

---

<div class="post-metadata">

**Author:** ![DanKolle](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dankolle/32/40_2.png) [@DanKolle](https://forum.juce.com/u/DanKolle)\
**Post date:** [February 5, 2015, 6:55pm UTC](https://forum.juce.com/t/leakedobjectdetector-hits-before-systemrequestedquit-is-called/14228/11 "2015-02-05T18:55:02Z")

</div>

Okay thanks for the info.

&nbsp;

I do not know if this is a hint but when I add the following observer:

```
void MyCallBack (CFNotificationCenterRef center,
                 void *observer,
                 CFStringRef name,
                 const void *object,
                 CFDictionaryRef userInfo)
{ NSLog(@"name: %@", name);NSLog(@"userinfo: %@", userInfo);
}

        CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(),
                                        NULL,
                                        MyCallBack,
                                        NULL,
                                        NULL,
                                        CFNotificationSuspensionBehaviorDeliverImmediately);
```

I receive all the notifies.

Could this be related to if Carbon or Cocoa is used?

I use Cmake to generate my projects so maybe a flag is not correctly set.

&nbsp;

Thanks  
Dan

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [February 5, 2015, 7:07pm UTC](https://forum.juce.com/t/leakedobjectdetector-hits-before-systemrequestedquit-is-called/14228/12 "2015-02-05T19:07:02Z")

</div>

Maybe. Like I say, if you've got a custom build&nbsp;and are bootstrapping things your own way then I can't really help much.

---

<div class="post-metadata">

**Author:** ![DanKolle](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dankolle/32/40_2.png) [@DanKolle](https://forum.juce.com/u/DanKolle)\
**Post date:** [February 5, 2015, 10:17pm UTC](https://forum.juce.com/t/leakedobjectdetector-hits-before-systemrequestedquit-is-called/14228/13 "2015-02-05T22:17:07Z")

</div>

I am aware of that but no worries.

After comparing the different code paths again I noticed that the Macintosh requires&nbsp;initialiseNSApplication() to be called which I missed.

Things are working now as expected.

Thanks again for the help.
