igorski / MWEngine

Audio engine and DSP library for Android, written in C++ providing low latency performance within a musical context, while providing a Java/Kotlin API. Supports both OpenSL and AAudio.
MIT License
257 stars 45 forks source link

Crashes when deleting SynthEvents #139

Closed vustav closed 2 years ago

vustav commented 2 years ago

Sometimes when I delete entire "tracks" in my app it crashes:

2021-12-11 09:02:51.477 1332-1332/? A/DEBUG: 2021-12-11 09:02:51.477 1332-1332/? A/DEBUG: Build fingerprint: 'google/sdk_gphone_x86/generic_x86:8.0.0/OSR1.170901.043/4456315:user/release-keys' 2021-12-11 09:02:51.477 1332-1332/? A/DEBUG: Revision: '0' 2021-12-11 09:02:51.477 1332-1332/? A/DEBUG: ABI: 'x86' 2021-12-11 09:02:51.477 1332-1332/? A/DEBUG: pid: 1205, tid: 1214, name: FinalizerDaemon >>> com.kiefer <<< 2021-12-11 09:02:51.478 1332-1332/? A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x28 2021-12-11 09:02:51.478 1332-1332/? A/DEBUG: Cause: null pointer dereference 2021-12-11 09:02:51.478 1332-1332/? A/DEBUG: eax 00000000 ebx 87cfa8f0 ecx a7fec240 edx 9c119080 2021-12-11 09:02:51.478 1332-1332/? A/DEBUG: esi 00000000 edi 88778220 2021-12-11 09:02:51.478 1332-1332/? A/DEBUG: xcs 00000073 xds 0000007b xes 0000007b xfs 0000003b xss 0000007b 2021-12-11 09:02:51.478 1332-1332/? A/DEBUG: eip 87bd0e54 ebp 88778218 esp 887781f0 flags 00010246 2021-12-11 09:02:51.506 1332-1332/? A/DEBUG: backtrace: 2021-12-11 09:02:51.506 1332-1332/? A/DEBUG: #00 pc 000cfe54 /data/app/com.kiefer-d0lw3Eue-M9dOY67EZWmkQ==/lib/x86/libmwengine_wrapped.so (_ZN8MWEngine14BaseAudioEvent19removeFromSequencerEv+100) 2021-12-11 09:02:51.506 1332-1332/? A/DEBUG: #01 pc 000cf9b1 /data/app/com.kiefer-d0lw3Eue-M9dOY67EZWmkQ==/lib/x86/libmwengine_wrapped.so (_ZN8MWEngine14BaseAudioEventD1Ev+65) 2021-12-11 09:02:51.506 1332-1332/? A/DEBUG: #02 pc 000f6e30 /data/app/com.kiefer-d0lw3Eue-M9dOY67EZWmkQ==/lib/x86/libmwengine_wrapped.so (_ZN8MWEngine14BaseSynthEventD1Ev+96) 2021-12-11 09:02:51.506 1332-1332/? A/DEBUG: #03 pc 000f8fb3 /data/app/com.kiefer-d0lw3Eue-M9dOY67EZWmkQ==/lib/x86/libmwengine_wrapped.so (_ZN8MWEngine10SynthEventD1Ev+83) 2021-12-11 09:02:51.506 1332-1332/? A/DEBUG: #04 pc 000f9021 /data/app/com.kiefer-d0lw3Eue-M9dOY67EZWmkQ==/lib/x86/libmwengine_wrapped.so (_ZN8MWEngine10SynthEventD0Ev+49) 2021-12-11 09:02:51.506 1332-1332/? A/DEBUG: #05 pc 000a5d27 /data/app/com.kiefer-d0lw3Eue-M9dOY67EZWmkQ==/lib/x86/libmwengine_wrapped.so (Java_nl_igorski_mwengine_core_MWEngineCoreJNI_delete_1SynthEvent+87) 2021-12-11 09:02:51.506 1332-1332/? A/DEBUG: #06 pc 00037222 /data/app/com.kiefer-d0lw3Eue-M9dOY67EZWmkQ==/oat/x86/base.odex (offset 0xa000) 2021-12-11 09:02:51.506 1332-1332/? A/DEBUG: #07 pc 00094dff [anon:libc_malloc:9bf00000] ........................................................................................................................................................................................................

...what I do is .setDeletable(true) on all events -> remove all BaseProcessors from the chain -> .reset() and .delete() on the ProcessingChain -> .delete() on the Intrument.

igorski commented 2 years ago

Hm, odd place for it to crash. I'll have a look. Is the Sequencer by any chance running during this cleanup operation ?

vustav commented 2 years ago

No, not running

igorski commented 2 years ago

I see you were using the SynthEvent class. I noticed some issue where it's parent constructor would not call the BaseAudioEvent construct routine (now renamed to init for consistency with usages in subclasses).

The changes have just been pushed, can you verify whether this addressed your issue ?

vustav commented 2 years ago

Still getting the same crashes. I'm afraid this is all I can get from Logcat, but it's probably the same issues as before:

A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 13613 (FinalizerDaemon)

igorski commented 2 years ago

I notice now you also delete the ProcessingChain instance. The destructor of the ProcessingChain suffered from a regression which has been addressed in f7a25c14ac9c6d1cd614059a551020fb82176a36

Could you pull these changes and verify whether this alleviates the issue ? If not, could you provide the latest stack trace upon the crash as two different fixes in two different areas have been pushed and I'd like to isolate staring blindly at a related, but not root issue.

vustav commented 2 years ago

I've done some testing and I'm still getting crashes, but different:

2021-12-15 21:28:55.523 6781-6781/? A/DEBUG: 2021-12-15 21:28:55.523 6781-6781/? A/DEBUG: Build fingerprint: 'google/sdk_gphone_x86/generic_x86:8.0.0/OSR1.170901.043/4456315:user/release-keys' 2021-12-15 21:28:55.523 6781-6781/? A/DEBUG: Revision: '0' 2021-12-15 21:28:55.524 6781-6781/? A/DEBUG: ABI: 'x86' 2021-12-15 21:28:55.524 6781-6781/? A/DEBUG: pid: 6712, tid: 6721, name: FinalizerDaemon >>> com.kiefer.llppdrums <<< 2021-12-15 21:28:55.524 6781-6781/? A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x29 2021-12-15 21:28:55.524 6781-6781/? A/DEBUG: Cause: null pointer dereference 2021-12-15 21:28:55.524 6781-6781/? A/DEBUG: eax 00000001 ebx 917fd744 ecx 899caa70 edx aae32000 2021-12-15 21:28:55.524 6781-6781/? A/DEBUG: esi 899edf80 edi 926d951c 2021-12-15 21:28:55.524 6781-6781/? A/DEBUG: xcs 00000073 xds 0000007b xes 0000007b xfs 0000003b xss 0000007b 2021-12-15 21:28:55.524 6781-6781/? A/DEBUG: eip 91734f05 ebp 926d9308 esp 926d92e0 flags 00010202 2021-12-15 21:28:55.525 6781-6781/? A/DEBUG: backtrace: 2021-12-15 21:28:55.525 6781-6781/? A/DEBUG: #00 pc 00074f05 /data/app/com.kiefer.llppdrums-QjdBk7-ilpBUO5HmTwqFIw==/base.apk (offset 0xad5000) 2021-12-15 21:28:55.525 6781-6781/? A/DEBUG: #01 pc 00076161 /data/app/com.kiefer.llppdrums-QjdBk7-ilpBUO5HmTwqFIw==/base.apk (offset 0xad5000) 2021-12-15 21:28:55.525 6781-6781/? A/DEBUG: #02 pc 0006a484 /data/app/com.kiefer.llppdrums-QjdBk7-ilpBUO5HmTwqFIw==/base.apk (offset 0xad5000) 2021-12-15 21:28:55.525 6781-6781/? A/DEBUG: #03 pc 00042442 /data/app/com.kiefer.llppdrums-QjdBk7-ilpBUO5HmTwqFIw==/oat/x86/base.odex (offset 0x15000) 2021-12-15 21:28:55.525 6781-6781/? A/DEBUG: #04 pc 00031fff [anon:libc_malloc:aae00000] .....................................................................................................................................................................................................

..yesterday I switched from building on your example project to importing the aar to a new project. Don't know if that has anything to do with it but I thaught I should mention it.

vustav commented 2 years ago

The one above is from the emulator in android studio. Here's one from my phone:

2021-12-15 21:51:59.019 16793-16793/? A/DEBUG: 2021-12-15 21:51:59.019 16793-16793/? A/DEBUG: Build fingerprint: 'Ulefone/Armor_7_EEA/Armor_7:9/PPR1.180610.011/1577252479:user/release-keys' 2021-12-15 21:51:59.019 16793-16793/? A/DEBUG: Revision: '0' 2021-12-15 21:51:59.019 16793-16793/? A/DEBUG: ABI: 'arm64' 2021-12-15 21:51:59.019 16793-16793/? A/DEBUG: pid: 16468, tid: 16479, name: FinalizerDaemon >>> com.kiefer.llppdrums <<< 2021-12-15 21:51:59.019 16793-16793/? A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x4080000000000051 2021-12-15 21:51:59.019 16793-16793/? A/DEBUG: x0 0000007bcbb13420 x1 0000007bb8f89f68 x2 0000007bd3d34880 x3 7f35adc5c1aedfa9 2021-12-15 21:51:59.019 16793-16793/? A/DEBUG: x4 0000007bd3bd3048 x5 0000007bba4abaa8 x6 0000000000001314 x7 0000007bba4abaf8 2021-12-15 21:51:59.019 16793-16793/? A/DEBUG: x8 4080000000000001 x9 0000000000000001 x10 0000000000430000 x11 0000007bd3bd1688 2021-12-15 21:51:59.019 16793-16793/? A/DEBUG: x12 0000007c59d10390 x13 114ba29503913121 x14 0000007c59b8f000 x15 ffffffffffffffff 2021-12-15 21:51:59.019 16793-16793/? A/DEBUG: x16 0000007bb9038768 x17 0000007bb8f8900c x18 0000000000000018 x19 0000007bd3d34880 2021-12-15 21:51:59.019 16793-16793/? A/DEBUG: x20 0000000000000000 x21 0000007bd3ccc000 x22 0000007bba4abb80 x23 0000007bb9c86e8f 2021-12-15 21:51:59.019 16793-16793/? A/DEBUG: x24 0000000000000008 x25 0000007bba4ad588 x26 0000007bd3ccc0a0 x27 0000000000000002 2021-12-15 21:51:59.019 16793-16793/? A/DEBUG: x28 0000007bba4ab8b0 x29 0000007bba4ab860 2021-12-15 21:51:59.019 16793-16793/? A/DEBUG: sp 0000007bba4ab850 lr 0000007bb8f89f7c pc 0000007bb8f89040 2021-12-15 21:51:59.038 16793-16793/? A/DEBUG: backtrace: 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #00 pc 0000000000085040 /data/app/com.kiefer.llppdrums-Q1iMnZ6zQuTd-FKS-SftJA==/base.apk (offset 0x9d9000) (MWEngine::BaseAudioEvent::~BaseAudioEvent()+52) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #01 pc 0000000000085f78 /data/app/com.kiefer.llppdrums-Q1iMnZ6zQuTd-FKS-SftJA==/base.apk (offset 0x9d9000) (MWEngine::SampleEvent::~SampleEvent()+16) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #02 pc 00000000005557e0 /system/lib64/libart.so (art_quick_generic_jni_trampoline+144) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #03 pc 000000000054ca4c /system/lib64/libart.so (art_quick_invoke_static_stub+604) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #04 pc 00000000000cf730 /system/lib64/libart.so (art::ArtMethod::Invoke(art::Thread, unsigned int, unsigned int, art::JValue, char const)+232) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #05 pc 000000000027d298 /system/lib64/libart.so (art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread, art::ArtMethod, art::ShadowFrame, unsigned short, art::JValue)+344) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #06 pc 0000000000277274 /system/lib64/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod, art::Thread, art::ShadowFrame&, art::Instruction const, unsigned short, art::JValue)+948) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #07 pc 000000000051d68c /system/lib64/libart.so (MterpInvokeStatic+204) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #08 pc 000000000053ef94 /system/lib64/libart.so (ExecuteMterpImpl+14612) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #09 pc 000000000028f37c /dev/ashmem/dalvik-classes.dex extracted in memory from /data/app/com.kiefer.llppdrums-Q1iMnZ6zQuTd-FKS-SftJA==/base.apk (deleted) (nl.igorski.mwengine.core.SampleEvent.delete+36) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #10 pc 00000000002510c0 /system/lib64/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.420220637+496) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #11 pc 00000000002568f0 /system/lib64/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread, art::CodeItemDataAccessor const&, art::ShadowFrame, art::JValue)+216) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #12 pc 0000000000277258 /system/lib64/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod, art::Thread, art::ShadowFrame&, art::Instruction const, unsigned short, art::JValue)+920) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #13 pc 000000000051c188 /system/lib64/libart.so (MterpInvokeVirtual+584) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #14 pc 000000000053ee14 /system/lib64/libart.so (ExecuteMterpImpl+14228) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #15 pc 000000000028f3b4 /dev/ashmem/dalvik-classes.dex extracted in memory from /data/app/com.kiefer.llppdrums-Q1iMnZ6zQuTd-FKS-SftJA==/base.apk (deleted) (nl.igorski.mwengine.core.SampleEvent.finalize) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #16 pc 00000000002510c0 /system/lib64/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.420220637+496) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #17 pc 00000000002568f0 /system/lib64/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread, art::CodeItemDataAccessor const&, art::ShadowFrame, art::JValue)+216) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #18 pc 0000000000277258 /system/lib64/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod, art::Thread, art::ShadowFrame&, art::Instruction const, unsigned short, art::JValue)+920) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #19 pc 000000000051c188 /system/lib64/libart.so (MterpInvokeVirtual+584) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #20 pc 000000000053ee14 /system/lib64/libart.so (ExecuteMterpImpl+14228) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #21 pc 00000000000aec58 /system/framework/boot-core-libart.vdex (java.lang.Daemons$FinalizerDaemon.doFinalize+22) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #22 pc 00000000002510c0 /system/lib64/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.420220637+496) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #23 pc 00000000002568f0 /system/lib64/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread, art::CodeItemDataAccessor const&, art::ShadowFrame, art::JValue)+216) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #24 pc 0000000000277258 /system/lib64/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod, art::Thread, art::ShadowFrame&, art::Instruction const, unsigned short, art::JValue)+920) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #25 pc 000000000051d4c8 /system/lib64/libart.so (MterpInvokeDirect+296) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #26 pc 000000000053ef14 /system/lib64/libart.so (ExecuteMterpImpl+14484) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #27 pc 00000000000aed40 /system/framework/boot-core-libart.vdex (java.lang.Daemons$FinalizerDaemon.runInternal+164) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #28 pc 00000000002510c0 /system/lib64/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.420220637+496) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #29 pc 00000000002568f0 /system/lib64/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread, art::CodeItemDataAccessor const&, art::ShadowFrame, art::JValue)+216) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #30 pc 0000000000277258 /system/lib64/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod, art::Thread, art::ShadowFrame&, art::Instruction const, unsigned short, art::JValue)+920) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #31 pc 000000000051c188 /system/lib64/libart.so (MterpInvokeVirtual+584) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #32 pc 000000000053ee14 /system/lib64/libart.so (ExecuteMterpImpl+14228) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #33 pc 00000000000aeb38 /system/framework/boot-core-libart.vdex (java.lang.Daemons$Daemon.run+20) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #34 pc 00000000002510c0 /system/lib64/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.420220637+496) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #35 pc 00000000002568f0 /system/lib64/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread, art::CodeItemDataAccessor const&, art::ShadowFrame, art::JValue)+216) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #36 pc 0000000000277258 /system/lib64/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod, art::Thread, art::ShadowFrame&, art::Instruction const, unsigned short, art::JValue)+920) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #37 pc 000000000051d100 /system/lib64/libart.so (MterpInvokeInterface+1392) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #38 pc 000000000053f014 /system/lib64/libart.so (ExecuteMterpImpl+14740) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #39 pc 00000000000ca8fe /system/framework/boot.vdex (java.lang.Thread.run+12) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #40 pc 00000000002510c0 /system/lib64/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.420220637+496) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #41 pc 000000000050ccf0 /system/lib64/libart.so (artQuickToInterpreterBridge+1032) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #42 pc 00000000005558fc /system/lib64/libart.so (art_quick_to_interpreter_bridge+92) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #43 pc 000000000054c788 /system/lib64/libart.so (art_quick_invoke_stub+584) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #44 pc 00000000000cf710 /system/lib64/libart.so (art::ArtMethod::Invoke(art::Thread, unsigned int, unsigned int, art::JValue, char const)+200) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #45 pc 0000000000454a40 /system/lib64/libart.so (art::(anonymous namespace)::InvokeWithArgArray(art::ScopedObjectAccessAlreadyRunnable const&, art::ArtMethod, art::(anonymous namespace)::ArgArray, art::JValue, char const)+104) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #46 pc 0000000000455b08 /system/lib64/libart.so (art::InvokeVirtualOrInterfaceWithJValues(art::ScopedObjectAccessAlreadyRunnable const&, _jobject, _jmethodID, jvalue)+424) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #47 pc 00000000004806fc /system/lib64/libart.so (art::Thread::CreateCallback(void)+1268) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #48 pc 0000000000082f44 /system/lib64/libc.so (__pthread_start(void)+36) 2021-12-15 21:51:59.039 16793-16793/? A/DEBUG: #49 pc 0000000000023324 /system/lib64/libc.so (__start_thread+68)

igorski commented 2 years ago

Does this still occur if you pull the latest changes added in 950473c8c102ef1d7a8bfa1d3d17bfae78ccde81 and rebuild the engine ?

vustav commented 2 years ago

Not a single crash in 5 mins of manually adding/removing tracks! Other than updating the engine I changed from setDeletable(true) to delete() when deleting events.

igorski commented 2 years ago

Excellent news!

Yes, the original naming of setDeletable was unfortunate... 😅 the new equivalent enqueueRemoval indicates more clearly its purpose (removing it from playback by the sequencer). I also updated the Wiki pages for more info on the function - and how it shouldn't actually be called outside of the inner engine -. Your use of delete() is the correct one in your scenario.