c++debuggingopencvstack-corruption

OpenCV: Stack Corruption with Unused Variable


Question up Front

Why is the simple inclusion of SegmentedBounds causing stack corruption? It is never used (yet), and I can continue through the program after the corruption warning without any apparent adverse effects.


Details

I have an OpenCV 2.4.10 project in Visual Studio 2013, which has suddenly started causing a stack corruption error:

Run-Time Check Failure #2 - Stack around the variable 'was_processed' was corrupted!

Interestingly, this is part of the OpenCV framework, not my code. It fails inside of sources/modules/highgui/src/window_w32.cpp in the WindowProc() function.

I can continue through the program with no apparent adverse effects, though.

Here is their code:

static LRESULT CALLBACK WindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
    LRESULT ret;

    if( hg_on_preprocess )
    {
        int was_processed = 0;
        int rethg = hg_on_preprocess(hwnd, uMsg, wParam, lParam, &was_processed);
        if( was_processed )
            return rethg;
    }
    ret = HighGUIProc(hwnd, uMsg, wParam, lParam);

    if(hg_on_postprocess)
    {
        int was_processed = 0;
        int rethg = hg_on_postprocess(hwnd, uMsg, wParam, lParam, &was_processed);
        if( was_processed )
            return rethg;
    }

    return ret;
} // <================= Debugger says it stopped here

Here is my code:

class ImageVariants {
public:
    Mat getOriginal() const;
    ...

private:
    Point3i Bounds3D;
    SubjectFace Face;
    Mat Mask;
    Mat Original;
    Bounds SegmentedBounds; // PROBLEMS
    Bounds Subject;

    bool boundsSet = false;
};

What fails:

Here is the kicker, SegmentedBounds is only declared and not (yet) used anywhere. Commenting it out "fixes" this problem.

Also, if the variable is of type Bounds* there is no corruption. It looks like if the memory footprint of ImageVariants exceeds a certain size, then we get problems.

This also happened before, under the exact same circumstances when the STL vector<> was declared and not used in the same class. I just removed it though, since I didn't need it.

How it is triggered:

I used OpenCV's HighGUI tools to interact with the program. Whenever I access a function from the ImageVariants class, it appears to bork:

Scalar color(255.0, 0.0, 0.0);
Point end(x, y);

//Draw the rectangle the user is drawing
Mat img = w->Segmenter.Variants.getOriginal();

rectangle(img, w->MouseStart, end, color, 1, 8, 0);
w->draw(&img);

getOriginal() just returns Original from the ImageVariants class. It doesn't access, set, or use SegmentedBounds in any way.

What I know:


Solution

  • After about two weeks of struggling, I found the answer. Apparently, some features of some OpenCV modules require you to call a function to initialize the module before using it:

    initModule_<module name>();
    

    I hadn't mentioned this in my question, but I was using the features2d module in this class. Specifically I was using the KeyPoints object from that library. So, in the constructor I added:

    initModule_features2d();
    

    All of my problems went away, no strangeness needed. I'm not sure why these functions are totally undocumented, as I can't find anywhere what they even do, but they work.

    Credit: nikk from this post made me aware of these function and saved my bacon, big time.