Skip to content

SEGFAULT in FFI to C++ library

A binding to OpenCV 2.2 I've been developing works fine on Mac OS X 10.5 and 10.6, but segfaults in Ubuntu 11.04 (32-bit) and Windows 7. On both Ubuntu and Windows, building a Debug or RelWithDebInfo build of OpenCV 2.2 seems to fix the problem. On Ubuntu, OpenCV 2.1 does not have this issue; I have not tried 2.1 on Windows.

To separate the bindings as a source of trouble, I boiled a test case down to a C function void myDilate(void) that allocates two images which are then passed to an OpenCV image processing routine, cvDilate. The myDilate function is defined in MyWrap.c. A shell C program, CTest2.c, defines a main function that calls this function. Building that program with,

gcc MyWrap.c CTest2.c -lopencv_core -lopencv_imgproc -o a.out

or

ghc -no-hs-main MyWrap.c CTest2.c -lopencv_core -lopencv_imgproc -o a.out

produces an executable that runs to completion.

Replacing the C main function with a Haskell main, Test2.hs, that calls the same function imported via the FFI results in an executable that segfaults. GDB + heavy printf'ing lead to a first crash within OpenCV at an assignment to a local that invokes a copy constructor. Changing this line to some other kind of initialization moves the segfault to another seemingly innocuous location within OpenCV. On the Windows 7 test machine, the segfault occurs in a different place, but also in OpenCV.

I have tested this with GHC 7.0.3 and 7.0.3.20110531. The version of gcc on the linux machine is 4.5.2.

The OpenCV libraries are .dylib on Mac, .so on Linux, and .dll on Windows. Of note is that OpenCV underwent a fairly large reorganization between 2.1 and 2.2, and is ever more heavily based around templatized C++.

I realize that OpenCV is a large dependency for a ticket, so I am willing to run any suggested tests.

Trac metadata
Trac field Value
Version 7.0.3
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler (FFI)
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information