PDA

View Full Version : dll loading trouble



ldynasa
23rd February 2011, 14:22
Here I've got a dll written in C downloaded from the internet. The library is successfully loaded in Qt, functions resolved. But calling these methods causes the app to crash with "Segmentation fault". Besides, I've built a C# project in VS08 and loaded the library. Everything works again.

Here's the code:
1.dll library:

void fdlib_detectfaces(unsigned char *imagedata, int imagewidth, int imageheight, int threshold);

2.Qt app:


ushort graydata[10*10];
QLibrary mylib("\\fdlib.dll");
bool okLoad = mylib.load();
bool loaded = mylib.isLoaded();
typedef void (*FdetectFace)(ushort*,int,int,int);
FdetectFace detectFace = (FdetectFace)mylib.resolve("fdlib_detectfaces");

//app crashes here
detectFace(&graydata[0],10,10,0);

3.C# implementation:

[DllImport("fdlib.dll")]

unsafe public static extern void fdlib_detectfaces(ushort*data,int w,int h,int threshold);

public static void TestFace()
{
unsafe
{
UInt16[] image = new ushort[10 * 10];
fixed (ushort* ptr = &image[0])
{
fdlib_detectfaces(ptr, 10, 10, 0);
}
}
}

high_flyer
23rd February 2011, 14:43
see comments in code:



ushort *graydata = NULL; //[10*10]; no need to initialize since detecFaces allocates the memory.
QLibrary mylib("\\fdlib.dll");
bool okLoad = mylib.load();
//You should really check for errors!
if(!okLoad){
//handle error
return;
}
bool loaded = mylib.isLoaded();
if(!loaded){
//TDOD: handle error
return;
}
typedef void (*FdetectFace)(ushort*,int,int,int);
FdetectFace detectFace = (FdetectFace)mylib.resolve("fdlib_detectfaces");
if(detectFace ){
//app crashes here
detectFace(graydata,10,10,0);
}

//at some point you should release the data allocted in detectFace:
if(graydata)
delete graydata;




unsafe public static extern void fdlib_detectfaces(ushort*data,int w,int h,int threshold);

unsafe
{
UInt16[] image = new ushort[10 * 10];
fixed (ushort* ptr = &image[0])
{
//app works fine here
fdlib_detectfaces(ptr, 10, 10, 0); //this calls recursively - does nothing, and never ends!! which is probably why it crashes.
}
}

ldynasa
23rd February 2011, 14:52
Thanx for the reply!
I've checked the loading status in the debug session so I've omitted the checking codes.

Besides, I've updated the last part to avoid confusion:D

high_flyer
23rd February 2011, 15:05
Again, your detectFace function does nothing except call its self endlessly - that is why it is crashing.