opencv / opencv_contrib

Repository for OpenCV's extra modules
Apache License 2.0
9.43k stars 5.76k forks source link

CvRTrees crashes on destructor when making an object copy #3679

Open opencv-pushbot opened 9 years ago

opencv-pushbot commented 9 years ago

Transferred from http://code.opencv.org/issues/2835

|| Nghia Ho on 2013-02-26 09:03
|| Priority: Normal
|| Affected: branch '2.4'
|| Category: ml
|| Tracker: Bug
|| Difficulty: None
|| PR: 
|| Platform: None / None

CvRTrees crashes on destructor when making an object copy

CvRTrees crashes on destruction if a copy variable of it is made. My guess is that they point to the same region of memory, instead of allocating new memory in a deep copy manner, and crashes when the two objects try to release the same memory. Here is a sample code that will cause the crash:

<pre>
int main()
{
    CvRTrees forest;

    // Generate some data for training, the values are not important
    cv::Mat training(100, 2, CV_32F);
    cv::Mat labels(100, 1, CV_32S);

    for(int i=0; i < 100; i++) {
        training.at<float>(i,0) = rand()/(1.0+RAND_MAX);
        training.at<float>(i,1) = rand()/(1.0+RAND_MAX);

        if(i % 2 == 0) {
            labels.at<int>(i) = 1;
        }
        else {
            labels.at<int>(i) = 0;
        }
    }

    forest.train(training, CV_ROW_SAMPLE, labels);

    CvRTrees copy_forest = forest;

    return 0;
}
</pre>

Output from GDB

<pre>
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff774dc4f in CvRTrees::clear() ()
</pre>

History

Vladislav Vinogradov on 2013-04-01 09:14
-   Description changed from CvRTrees crashes on destruction if a copy
    variable of it is made. My guess is... to CvRTrees crashes on
    destruction if a copy variable of it is made. My guess is... More
Vladislav Vinogradov on 2013-04-01 09:30
@CvRTrees@ doesn't have appropriate copying constructor and assignment operator.
-   Assignee set to Maria Dimashova
-   Category set to ml
Durant35 commented 6 years ago

It seems that CvRTrees copy_forest = forest; is not a deep copy, thus share some pointers. One release first, the other release failed and cause memory crash.