0

I'm trying to use phash library using this wrapper https://github.com/ludoviclefevre/phash-integration-in-csharp

c++ header:

int ph_dct_imagehash(const char* file,ulong64 &hash)

c# dll import:

DllImport(@"pHash.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int ph_dct_imagehash(string file, ref ulong hash);

test code:

ulong hash = 0; 
foreach (var file in files) 
{
    ph_dct_imagehash(file, ref hash);

    dictionary.Add(file, hash);
}

It works perfectly for few picture, but when ther's about 200-300 pictures i got Accesviolation exception

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

My first lead is garbage collector but i'm confused.. should i use intpr instead of string and hash parameters? I tried changing ref parameter to out but it doesn't matter..

  • 2
    http://stackoverflow.com/a/30028680/1870760 instruct the Marshaller how it should pass the file argument with `[MarshalAs(UnmanagedType.LPStr)]`. – Hatted Rooster Oct 06 '16 at 12:05
  • No, the declaration is correct and LPStr is already the default. You need to debug the native code to find out why it bombed. Project > Properties > Debugging > tick "Enable native code debugging". A memory leak is not unlikely btw, error checking is rudimentary in that library so a failed malloc() will AVE. Also easily caused by your own code, forgetting to use Image.Dispose() in a C# program is a very traditional bug. – Hans Passant Oct 06 '16 at 13:57

0 Answers0