Menu

PocketSphinx – error when calling ps_unset_search

Help
Oren G.
2018-01-22
2018-01-28
  • Oren G.

    Oren G. - 2018-01-22

    During recognition I have to switch between grammars. After using a grammar, I don’t need it anymore, so I’m trying to free the last used grammar. This is the code I use:

    ps_end_utt(decoder)
    
    name_ =  ps_get_search(decoder)
    result = ps_unset_search(decoder, name_)
    ps_set_jsgf_file(decoder, "2ndGrammar", "c:\sphinx\2ndGrammar.jsgf")
    ps_set_search(decoder, "2ndGrammar")
    
    ps_start_utt(decoder)
    

    The code above does switch between the grammars, but I have two issues:

    1. ps_unset_search always returns an error (-1).
    2. When I finally call ps_free, the app crashes. Visual Studio gives the error:
      “A heap has been corrupted”. The error occur at the function ps_search_base_free, at the first line - ckd_free(search->name); Regarding the “name” variable, Visual Studio indicates “error reading characters of string”.

    When I don’t use the ps_set_search api, I don’t have any issues with ps_free.

    PS – I’m using PocketSphinx with C# binding, on Windows

     
    • Nickolay V. Shmyrev

      I'm sorry, it is hard to understand what is going on. You write you use C# with bindings but your code in C.

      I've just tried the following code and it works fine

      using System;
      using System.IO;
      using Pocketsphinx;
      
      public class Test
      {
         public static void Main()
         {
          Config c = Decoder.DefaultConfig();
              c.SetString("-hmm", "../../model/en-us/en-us");
              c.SetString("-lm", "../../model/en-us/en-us.lm.bin");
              c.SetString("-dict", "../../model/en-us/cmudict-en-us.dict");
          Decoder d = new Decoder(c);
      
          byte[] data = File.ReadAllBytes("../../test/data/goforward.raw");
          d.StartUtt();
          d.ProcessRaw(data, data.Length, false, false);
          d.EndUtt();
          Console.WriteLine("Result is '{0}'", d.Hyp().Hypstr);
          foreach (Segment s in d.Seg()) {
              Console.WriteLine(s);
          }
      
          string name =  d.GetSearch();
          Console.WriteLine("Search is {0}", name);
              d.UnsetSearch(name);
              d.SetLmFile("another_search", "../../model/en-us/en-us.lm.bin");
              d.SetSearch("another_search");
          name =  d.GetSearch();
          Console.WriteLine("Search is {0}", name);
      
          data = File.ReadAllBytes("../../test/data/goforward.raw");
          d.StartUtt();
          d.ProcessRaw(data, data.Length, false, false);
          d.EndUtt();
          Console.WriteLine("Result is '{0}'", d.Hyp().Hypstr);
         }
      }
      

      Can you please provide a better example to reproduce your issue?

       
      • Oren G.

        Oren G. - 2018-01-27

        I have found that the error was caused by the C# binding. By this code:

        [DllImport("pocketsphinx.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern string ps_get_search(IntPtr decoder);
        

        The error was fixed after changing the returned type from String to IntPtr.

         
        • Nickolay V. Shmyrev

          I recommend you to use our official swig-created bindings.

           
          • Oren G.

            Oren G. - 2018-01-28

            I didn't know how to use them. There are no instructions. It took me more time to search the internet for the info on how to use them (and at the end I still don't know how) then to write my own PInvoke signatures, which work well.

             

            Last edit: Oren G. 2018-01-28

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.