Calling Pocketsphinx in C # AccesViolationException
I'm trying to do a pocketsphinx tutorial in C # using pinvoke, but get an AccessViolationException when I try to decode with ps_decode_raw ().
IntPtr ps = PocketSphinx.ps_init(config);
IntPtr fh = Win32Util.fopen(@"goforward.raw", "rb");
int rv = PocketSphinx.ps_decode_raw(ps, fh, "goforward", -1);
The functions are wrapped like this
//ps_decoder_t* ps_init(cmd_ln_t* config)
[DllImport("pocketsphinx.dll",
SetLastError = true,
CallingConvention = CallingConvention.Cdecl)]
public extern static IntPtr ps_init(
IntPtr config);
//int ps_decode_raw(ps_decoder_t *ps, FILE *rawfh, char const *uttid, long maxsamps);
[DllImport("pocketsphinx.dll",
SetLastError = true,
CallingConvention = CallingConvention.Cdecl)]
public extern static int ps_decode_raw(
IntPtr ps,
IntPtr rawfh,
[MarshalAs(UnmanagedType.LPStr)] string uttid,
int maxsamps);
[DllImport("msvcrt.dll",
SetLastError = true,
CallingConvention = CallingConvention.Cdecl)]
public extern static IntPtr fopen(
[MarshalAs(UnmanagedType.LPStr)] string _Filename,
[MarshalAs(UnmanagedType.LPStr)] string _Mode);
I wrapped C fopen, but because it was the fastest way I could get started implementing the tutorial.
I've tried calling cmd_ln_retain on ps to make sure ps isn't causing the problem. (this is not true). I also removed my debug code in the above.
I'm pretty sure something is wrong with fopen, but I'm not sure what.
Someone asked for pocketsphinx log. https://justpaste.it/h52t
source to share
You are not checking anything for errors. And it is wrong to set SetLastError
these functions to true. They won't name SetLastError
.
Your big problem is that the library uses a specific instance of the C runtime, depending on how you created it. And your import fopen
comes from another instance of the C runtime.
You need to add code to a library that provides functions for creating and destroying objects FILE*
. By doing this, you will get FILE*
generated with the correct runtime.
source to share