[C++-sig] Weird problem with opaque pointers and pointers by reference

Giuseppe Corbelli giuseppe.corbelli at copanitalia.com
Mon Sep 24 16:21:54 CEST 2012


Hi all
I am facing an "interesting" problem. I have factory functions implemented in
C++ that create opaque pointers. Such pointers are tossed around in python and
finally freed in C++. The module is implemented using boost::python.
Platform gcc 4.6.3/debian/boost 1.46, but also tried on msvc8/win7/boost 1.43

The opaque pointer refers to an IplImage, a C struct defined by OpenCV image
processing library.

I'm attaching a complete test case, however, in short:

IplImage* CreateImage()
{
    IplImage* img = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
    std::cout << "Created " << img << std::endl;
    return img;
}

void ReleaseImage(IplImage*& img)
{
    std::cout << "Release Image " << img << std::endl;
    ::cvReleaseImage(&img);
}

When I run:
img = CameraConnect_Fake_Python.CreateImage()
CameraConnect_Fake_Python.ReleaseImage(img)

here's the output:
Created 0x8e34970
Release Image 0x70
Segmentation fault

For some reason that I don't grasp the reference-to-pointer is not passed
around correctly.

Still more interesting, if I try:

struct OPAQUE  {int unused;};

OPAQUE* factory()
{
    return new OPAQUE();
}

void destroyer(OPAQUE*& x)
{
    delete (x);
    x = NULL;
}
img = CameraConnect_Fake_Python.FACTORY()
CameraConnect_Fake_Python.DESTROYER(img)

It runs correctly!
Hope someone can help...
-- 
            Giuseppe Corbelli
WASP Software Engineer, Copan Italia S.p.A
Phone: +390303666318  Fax: +390302659932
E-mail: giuseppe.corbelli at copanitalia.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: main.cpp
Type: text/x-c++src
Size: 1437 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20120924/959123a5/attachment.cpp>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.py
Type: text/x-python
Size: 367 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20120924/959123a5/attachment.py>


More information about the Cplusplus-sig mailing list