Hi all,

I have a possible fix for issue #37 (crash on startup). After closer inspection (attached to the issue :) ), it looks like the reason Hydrogen crashes on startup is because ALSA MIDI driver calls Hydrogen::get_instance() while first Hydrogen::get_instance() (which creates the singleton object) is still running. This causes two Hydrogen objects to be created, which is responsible for the crash.

First fix (mine) is very simple - set the __instance variable before the drivers are started. See: hydrogen-crash-patch.diff - the drawback is that something (MIDI or audio driver) may still potentially call things on Hydrogen object before it's fully constructed.

Second fix: Torben Hohn suggested on #lad IRC channel to use a mutex in get_instance() to prevent situations like this. I've found a more lightweight way - ensuring that MIDI thread doesn't proceed until the drivers are fully set up. It's actually quite easy to do, as startAudioDrivers locks the audio engine until it completes. Because MIDI thread is started from that function (with audio engine mutex locked), it's enough to lock and unlock the audio engine once from MIDI thread (so it doesn't start doing anything on Hydrogen object until driver initialization stuff completes and unlocks the mutex).

I did this in the second patch (hydrogen-crash-patch-improvement) for ALSA MIDI only, but the same fix should probably be applied to other MIDI drivers (and possibly ALSA audio driver as well, just to be safe - there is no point in doing any processing until the driver initialization is complete).

More details (much more than anyone is willing to digest, I guess) are added as comments to the ticket.

Any comments/suggestions/mistakes in the analysis above? (I'm still mostly unfamiliar with Hydrogen code, so might have missed something important)

Krzysztof

Index: libs/hydrogen/src/hydrogen.cpp
===================================================================
--- libs/hydrogen/src/hydrogen.cpp	(revision 767)
+++ libs/hydrogen/src/hydrogen.cpp	(working copy)
@@ -1719,6 +1719,8 @@
 	hydrogenInstance = this;
 // 	__instance = this;
 	audioEngine_init();
+    // Prevent double creation caused by calls from MIDI thread
+    __instance = this;
 	audioEngine_startAudioDrivers();
 
 }
Index: libs/hydrogen/src/IO/alsa_midi_driver.cpp
===================================================================
--- libs/hydrogen/src/IO/alsa_midi_driver.cpp	(revision 767)
+++ libs/hydrogen/src/IO/alsa_midi_driver.cpp	(working copy)
@@ -29,6 +29,7 @@
 
 #include <hydrogen/globals.h>
 #include <hydrogen/event_queue.h>
+#include <hydrogen/audio_engine.h>
 
 #include <pthread.h>
 
@@ -56,6 +57,10 @@
 {
 	AlsaMidiDriver *pDriver = ( AlsaMidiDriver* )param;
 	_INFOLOG( "starting" );
+    
+        // wait until audioEngine_startAudioDrivers sets Hydrogen::__instance (then it releases the lock)
+        AudioEngine::get_instance()->lock("alsaMidiDriver_thread");
+        AudioEngine::get_instance()->unlock();
 
 	if ( seq_handle != NULL ) {
 		_ERRORLOG( "seq_handle != NULL" );
------------------------------------------------------------------------------
Create and Deploy Rich Internet Apps outside the browser with Adobe(R)AIR(TM)
software. With Adobe AIR, Ajax developers can use existing skills and code to
build responsive, highly engaging applications that combine the power of local
resources and data with the reach of the web. Download the Adobe AIR SDK and
Ajax docs to start building applications today-http://p.sf.net/sfu/adobe-com
_______________________________________________
Hydrogen-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/hydrogen-devel

Reply via email to