danomatika / ofxPd

(maintained) a Pure Data addon for OpenFrameworks using libpd
Other
202 stars 45 forks source link

pdMultiExample crashes with PDINSTANCE and PDTHREADS #82

Closed Ant1r closed 3 years ago

Ant1r commented 3 years ago

Ubuntu 16.04, of_v0.9.8_linux64 gdb backtrace of the Debug build:

Thread 1 "pdMultiExample" received signal SIGSEGV, Segmentation fault.
0x00000000004a5801 in pd_typedmess ()
(gdb) bt
#0  0x00000000004a5801 in pd_typedmess ()
#1  0x0000000000504042 in binbuf_eval ()
#2  0x0000000000542f18 in text_template_init ()
#3  0x00000000004a5435 in pdinstance_new ()
#4  0x0000000000456dcf in ofApp::setup() ()
#5  0x00000000005b35c3 in std::_Function_handler<bool (void const*, ofEventArgs&), std::shared_ptr<of::priv::Function<ofEventArgs, std::recursive_mutex> > ofEvent<ofEventArgs, std::recursive_mutex>::make_function<ofBaseApp>(ofBaseApp*, void (ofBaseApp::*)(ofEventArgs&), int)::{lambda(void const*, ofEventArgs&)#1}>::_M_invoke(std::_Any_data const&, void const*&&, ofEventArgs&) ()
#6  0x000000000055f5a8 in ofCoreEvents::notifySetup() ()
#7  0x00000000005b2fea in ofMainLoop::run(std::shared_ptr<ofBaseApp>) ()
#8  0x000000000059fd5d in ofRunApp(std::shared_ptr<ofBaseApp>) ()
#9  0x00000000005a34ff in ofRunApp(ofBaseApp*) ()
#10 0x000000000041fc1c in main ()
danomatika commented 3 years ago

What version of ofxPd? master or a tag? Also, the multi example is really only experimental...

Ant1r commented 3 years ago

master, with some minor fixes like: pdinstance1 = libpd_free_instance(); -> libpd_free_instance(pdinstance1); and adapting ofSoundStreamSetup() to old OF way.

multi example is really only experimental

I know that :-) I would like to see if I could have it running anyway. libpd/samples/c/pdtest_multi doesn't crash, so it should be able to (at least) run.

danomatika commented 3 years ago

I believe I made most of those changes in a commit yesterday. Did I miss something...

Ant1r commented 3 years ago

ok maybe i'm not up to date. let me see.

Ant1r commented 3 years ago

I wasn't... now I am. It compiles fine now, but same crash.

Ant1r commented 3 years ago

OK, the previous crash is fixed by calling libpd_init() before the first libpd_new_instance().

Now I have another one:

Thread 1 "pdMultiExample_" received signal SIGSEGV, Segmentation fault.
0x0000000000495fdc in sys_lock () at ../../../addons/ofxPd/libs/libpd/pure-data/src/s_inter.c:1641
1641        pthread_mutex_lock(&pd_this->pd_inter->i_mutex);
(gdb) bt
#0  0x0000000000495fdc in sys_lock () at ../../../addons/ofxPd/libs/libpd/pure-data/src/s_inter.c:1641
#1  0x0000000000542313 in libpd_message (recv=0x88c9cc "pd", msg=0x88c9c8 "dsp", argc=1, argv=0xecdf50) at ../../../addons/ofxPd/libs/libpd/libpd_wrapper/z_libpd.c:384
#2  0x0000000000542220 in libpd_finish_message (recv=0x88c9cc "pd", msg=0x88c9c8 "dsp") at ../../../addons/ofxPd/libs/libpd/libpd_wrapper/z_libpd.c:357
#3  0x000000000042c2af in pd::PdBase::PdContext::computeAudio (this=0xecc1f0, state=true) at /home/arsene/Documents/OF/of_v0.9.8_linux64_release/addons/ofxPd/libs/libpd/cpp/PdBase.hpp:1148
#4  0x0000000000435383 in pd::PdBase::computeAudio (this=0xecc0c0, state=true) at /home/arsene/Documents/OF/of_v0.9.8_linux64_release/addons/ofxPd/libs/libpd/cpp/PdBase.hpp:233
#5  0x000000000042e8df in ofxPd::computeAudio (this=0xecc0c0, state=true) at ../../../addons/ofxPd/src/ofxPd.cpp:175
#6  0x000000000042e9ba in ofxPd::start (this=0xecc0c0) at ../../../addons/ofxPd/src/ofxPd.cpp:179
#7  0x000000000042b626 in ofApp::setup (this=0xecc0a0) at /home/arsene/Documents/OF/of_v0.9.8_linux64_release/addons/ofxPd/pdMultiExample/src/ofApp.cpp:88
#8  0x000000000042a1bb in ofBaseApp::setup (this=0xecc0a0, args=...) at ../../../libs/openFrameworks/app/ofBaseApp.h:63
Ant1r commented 3 years ago

sorry, forget last crash (I had commented out the second libpd_new_instance() for debugging...).

so it works now!

the only thing is to call libpd_init() before the first libpd_new_instance(). Would you want I make a PR?

danomatika commented 3 years ago

Ah ok. I see I need to add the instance flags to the multi example makefile as the PG generated the Xcode project without them, so I wasn't seeing any run time crashes just bad sound as it was single instance.

Ant1r commented 3 years ago

Yeah, on Linux I add the flags in the config.make. I don't know if the PG takes it into account; if it does, then it might be useful to add such a config.make into the multiExample directory.

There's still an issue: both pdinstances need to call pd.init(...), which is not the case currently. So the second one, which is not fully configurated, cannot produce sound and cannot measure time.

So I copied pd.init(...) after libpd_set_instance(pdinstance2), and now both instances behave normally!

danomatika commented 3 years ago

Right. I was looking at this the other day and it seemed weird then, as compared to the C pdtest_multi.

I know now the reason was that I wasn't compiling with multi instance enabled. I will add an error check for this and some info in the readme about setting the flags manually for Xcode.

danomatika commented 3 years ago

Ok, check latest commits, mainly 4e7597d3e392f3406df1827e8969a60f3e9849a1. The example is working and you should get a mix of 440 and 880 sine tones.

You should be able to build the pdMultiExample with required flags with the makefile by default. For PG projects, uncomment the line in addons_config.mk.

Ant1r commented 3 years ago

Great, thanks Dan! btw you didn't add pdMultiExample/config.make, is it on purpose? As it's currently git-ignored, I'm wondering...

Anyway I'm quite happy, as I'm now able to compile ofxPd with the setup I needed:

I was able to verify that audio never stops, even when the first instance is hanging doing a huge job (like calculating log(2) 10 million times....). Still to confirm on mobile devices...

danomatika commented 3 years ago

Yeah, I forgot to add config.make as it was ignored. Now there.