Mutex and NSInternalInconsistencyException

I am using a mutex to prevent my application from being started multiple times.

On Mac using a compiled application, the initial launch everything works as intended, however, subsequent launches produce the below error when executing the “quit” command.

Any ideas?

*** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘NSApp with wrong _running count’
*** Call stack at first throw:
(
0 CoreFoundation 0x9017b471 __raiseError + 193
1 libobjc.A.dylib 0x96532091 objc_exception_throw + 162
2 CoreFoundation 0x9017b2f8 +[NSException raise:format:arguments:] + 136
3 Foundation 0x967d9377 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
4 AppKit 0x90c9306f -[NSApplication run] + 458
5 XojoFramework 0x0045bf27 enableMenuItems + 2007
6 XojoFramework 0x0045a5c1 RuntimeRun + 49
7 MyApp 0x001beaf1 REALbasic._RuntimeRun + 145
8 MyApp 0x00002a57 _Main + 257
9 MyApp 0x0000250c % main + 36
10 MyApp 0x002ab099 _start + 116
11 MyApp 0x002aafef start + 43
12 ??? 0x00000002 0x0 + 2
)

[quote=96932:@Jason Hel]I am using a mutex to prevent my application from being started multiple times.

On Mac using a compiled application, the initial launch everything works as intended, however, subsequent launches produce the below error when executing the “quit” command.
[/quote]
No need on OS X
It does this by default

The application if launched from the command line (requirement) will start up multiple instances of the application.

how are you starting it ?
using the open command or something else ?
and what kind of project did you start with ?
this almost look like a GUI app that is being started from a terminal with no window server attached or something

Just calling the program by the full path… …/Application/MyApp.app/Contents/MacOS/MyApp

The app gets called by other programs and I need to handle parameters but I dont want to spin up more than one concurrent application.

Basically I use the mutex to know that another instance is running and I handle the parameters and then shutdown the application.

My workaround is:
// for some reason this throws an exception when running as mutex
quit
Exception err as RuntimeException

when you say “called by other programs” headless or not ?

never mind my work around isnt working… still dumps a stacktrace…

In the App.open method I used the mutex:

appMutex = new Mutex(“com.foobar.MyApp”)

// grab command line args…
Dim args As String
dim commandArgs() as string
commandArgs = split(system.commandLine, " ")
if uBound(commandArgs) > 0 then
dim arg As String
arg = commandArgs(1)
handleArgument
end if

// try to get a lock on mutex
if not appMutex.TryEnter then
shutdown() // just calls quit
else
self.isMainApp = True
splashWindow.Show
end if

The app does not launch a window when it starts.

a gui app with no windows
that should die

As you can see in the “open” method of App I launch my app’s splash screen window. I just tried having the startup window being hidden and calling shutdown from there… same exception

Sure but you still haven’t said how you start this
From another app with or without a user slogged in ?
If its without one then a GUI app (desktop app) probably will not work as it requires window manager

[quote=97005:@Jason Hel]Just calling the program by the full path… …/Application/MyApp.app/Contents/MacOS/MyApp
[/quote]

You should not call a GUI app by launching the Unix process directly. Indeed, that creates multiple instances of the process.

Instead, use the Open command :

Open  ../Application/MyApp.app

That way OS X manages automatically the multiple launches.

I tried launching an app by calling the Unix executable inside from the terminal. It does not release the terminal until the application has quit or the process being killed from another window. The 'NSInternalInconsistencyException', reason: 'NSApp with wrong _running count' error may simply mean that the app.quit is using an inappropriate method to kill its own process.

However, I could not reproduce the error.

I am pretty sure calling an app by the Unix executable would not be sanctioned by Apple as a supported way of launching a GUI app. Better use Open <path to the .app>.

I attempted to use the open program on the mac and I saw the same error.

I believe I have found a solution to this, in the window where I was calling the quit I changed the code to the below and I am not seeing the exception.

// shutdown the app without crashing.
App.AutoQuit = True
self.Close