alphacep / vosk-api

Offline speech recognition API for Android, iOS, Raspberry Pi and servers with Python, Java, C# and Node
Apache License 2.0
8.06k stars 1.11k forks source link

Crash when calling `vosk_recognizer_free` #1261

Open tim-gromeyer opened 1 year ago

tim-gromeyer commented 1 year ago

About me and what I'm trying

Hello, my name is Tim Gromeyer and I'm trying to create a voice assistant using this library.

The problem

The recognition works really well and the models are small too. But there is something wrong with the code:
Every time I call vosk_recognizer_free the project crashes.
Here the line in the code, currently commented out: https://github.com/tim-gromeyer/VoiceAssistant/blob/main/src/recognizer.cpp#L325.

VoskRecognizer set up

I set up the VoskModel in a function called via std::thread(&myFunction).detach(). The recognizer is set up in the same function as the model but I also tried to set it up in the apps main thread which changed absolutely nothing.

Why I need help

I can't debug what's happening in vosk_recognizer_free because I simply doesn't have the necessary resources(memory).

System information

Edit:

Steps to reproduce:

  1. Install a recent version of git, cmake and Qt(I tested 5.15.3 and 6.4.2) with following components: Base, Network, Multimedia, (Translation tools)
  2. Clone the repo: git clone https://github.com/tim-gromeyer/VoiceAssistant
  3. Create build dir: mkdir -p VoiceAssistant/build && cd VoiceAssistant/build
  4. Build: cmake ..(if you miss some dependency I suggest apt-file), cmake --build . -j3
  5. Execute: ./voiceassistant

2nd edit: Finx formatting

Thank you all for developing such a nice library and also thanks in advance!

nshmyrev commented 1 year ago

Hi

It is hard to figure out what is wrong without binary in hand. It might be a memory corruption from some external code. I can suggest you the following to check the problem:

  1. Try to destroy recognizer without running recognition itself just after construction. Maybe it will work.
  2. Try to run under gdb and collect stacktrace. Share stacktrace.
  3. Try to run with valgrind and see if it reports some memory corruption. Share valgrind output
tim-gromeyer commented 1 year ago

I called vosk_recognizer_free directly after the construction. It still crashes. That's the code I used:

model = vosk_model_new(QString(modelDir() + lang).toUtf8());
if (model) {
    qDebug() << "[debug] Loaded model, language:" << lang;
    globalRecognizer = vosk_recognizer_new(model, 16000.0);
    vosk_recognizer_free(globalRecognizer);

Thank you for the quick response. It's my first time actually collecting a stacktrace. Always did it in QtCreator. So if I did something wrong you know why. But anyway here it is:

#0  0x000055555557b591 in Recognizer::~Recognizer() (this=0x7fffe04183c0, __in_chrg=<optimized out>)
    at /home/tim/qtprojegt/VoiceAssistant/src/recognizer.cpp:325
#1  0x00007ffff6369a5e in vosk_recognizer_free () at /home/tim/qtprojegt/VoiceAssistant/vosk/libvosk.so
#2  0x000055555557b5a8 in Recognizer::~Recognizer() (this=0x7fffe04183c0, __in_chrg=<optimized out>)
    at /home/tim/qtprojegt/VoiceAssistant/src/recognizer.cpp:326
#3  0x00007ffff6369a5e in vosk_recognizer_free () at /home/tim/qtprojegt/VoiceAssistant/vosk/libvosk.so
#4  0x000055555557b5a8 in Recognizer::~Recognizer() (this=0x7fffe04183c0, __in_chrg=<optimized out>)
    at /home/tim/qtprojegt/VoiceAssistant/src/recognizer.cpp:326
#5  0x00007ffff6369a5e in vosk_recognizer_free () at /home/tim/qtprojegt/VoiceAssistant/vosk/libvosk.so
#6  0x000055555557b5a8 in Recognizer::~Recognizer() (this=0x7fffe04183c0, __in_chrg=<optimized out>)
    at /home/tim/qtprojegt/VoiceAssistant/src/recognizer.cpp:326
#7  0x00007ffff6369a5e in vosk_recognizer_free () at /home/tim/qtprojegt/VoiceAssistant/vosk/libvosk.so
#8  0x000055555557b5a8 in Recognizer::~Recognizer() (this=0x7fffe04183c0, __in_chrg=<optimized out>)
    at /home/tim/qtprojegt/VoiceAssistant/src/recognizer.cpp:326
#9  0x00007ffff6369a5e in vosk_recognizer_free () at /home/tim/qtprojegt/VoiceAssistant/vosk/libvosk.so

Now I try to run it with valgrind.

tim-gromeyer commented 1 year ago

Ah valgrind is the tool named memcheck in QtCreator. I already tried to run it several times but after closing the window nothing happens. Waiting for 1/2 hour and still nothing happened. So vallgrind doesn't help here.

tim-gromeyer commented 1 year ago

Valgrind is already running for 36 minutes. I will let it run and go to bed.

nshmyrev commented 1 year ago
#0  0x000055555557b591 in Recognizer::~Recognizer() (this=0x7fffe04183c0, __in_chrg=<optimized out>)
    at /home/tim/qtprojegt/VoiceAssistant/src/recognizer.cpp:325

What is this? A trace with circular dependency? Maybe you can call your class something else, not Recognizer? Try it. We probably need namespace in vosk.

tim-gromeyer commented 1 year ago

It actually worked, funnily enough. Such a small detail I never would have thought of. Anyway, thank you very much!

nshmyrev commented 1 year ago

We need to introduce namespace in vosk sources, reopening for now