Search code examples
c++opencvubunturuntime-error

corrupted size vs. prev_size in C++


I am new to c++, I started with it today and I got an error that I can't solve myself.

When I run my code it sometimes works fine, and I sometimes get an error after a few seconds.

The errors that I have gotten are:

  • "corrupted size vs. prev_size Aborted (core dumped)"
  • "segmentation fault (core dumped)"

This is my code:

#include <opencv2/opencv.hpp> 
#include <iostream>
#include <random>

using namespace cv;
using namespace std;
using chrono::duration;
using chrono::duration_cast;
using chrono::high_resolution_clock;
using chrono::milliseconds;

class Product
{
public:
    Point location = Point(-80, 0);
    Scalar color;

    Product(int given_y, Scalar given_color)
    {
        location.y = given_y;
        color = given_color;
        cout << "Product created" << endl;
    }

    Mat update(double speed, Mat img, double dt)
    {
        location.x += speed* dt;
        circle(img, location, 25, Scalar(255, 255, 255), FILLED, LINE_8);
        circle(img, location, 20, color, FILLED, LINE_8);
        putText(img, "12", location + Point(-17, +7), FONT_HERSHEY_DUPLEX, .8, Scalar(255, 255, 255), 1, LINE_AA);
        return (img);
    }
};

int main(int argc, char **argv)
{
    // Constant variables
    const int kScreenWidth = 1920;
    const int kScreenHeight = 1080;
    const int kUpdateFpsMilliseconds = 600;
    const String kWindowName = "Moving dots";
    const Mat kBlackImg(kScreenHeight, kScreenWidth, CV_8UC3, Scalar(0, 0, 0));

    // Variables
    Scalar red = Scalar(255, 0, 0);
    Scalar green = Scalar(0, 255, 0);
    Scalar blue = Scalar(0, 0, 255);
    Scalar cyan = Scalar(0, 255, 255);
    Scalar magenta = Scalar(255, 0, 255);
    Scalar yellow = Scalar(255, 255, 0);
    Scalar black = Scalar(0, 0, 0);
    Scalar white = Scalar(255, 255, 255);

    vector<Product> products;
    Mat img;
    double speed = .2;
    double fps;
    int frame_counter = 0;
    int loop_dt;
    int time_since_fps_update = 0;
    int counter = 0;
    bool remove_product = false;
    bool full_screen = false;

    // Random number generators
    random_device rd_seed;
    mt19937 rng(rd_seed());
    uniform_int_distribution<mt19937::result_type> rd_screen_height(50, kScreenHeight - 50);
    uniform_int_distribution<mt19937::result_type> rd_new_product(0, 100);

    namedWindow(kWindowName, WINDOW_NORMAL);
    setWindowProperty(kWindowName, WND_PROP_FULLSCREEN, WINDOW_FULLSCREEN);

    auto loop_start = high_resolution_clock::now();

    while (true)
    {
        // Black image
        Mat img = kBlackImg.clone();

        // Add new products
        if (rd_new_product(rng) < .1 * loop_dt)
        {
            products.push_back(Product(rd_screen_height(rng), red));
        }

        // Update products
        if (products.size() > 0)
        {
            for (int i = 0; i <= products.size(); i++)
            {
                img = products[i].update(speed, img, loop_dt);
            }
        }

        // Show text and border
        stringstream temp;
        temp << "FPS: " << fps << " Products: " << products.size();
        putText(img, temp.str(), Point(18, 38), FONT_HERSHEY_DUPLEX, .8, white, 1, LINE_AA);
        rectangle(img, Point(6, 6), Point(kScreenWidth - 6, kScreenHeight - 6), white, 12);
        rectangle(img, Point(6, 6), Point(kScreenWidth - 6, 60 - 6), white, 12);
        rectangle(img, Point(6, 6), Point(kScreenWidth - 6, kScreenHeight - 6), cyan, 3);
        rectangle(img, Point(6, 6), Point(kScreenWidth - 6, 60 - 6), cyan, 3);

        // Update screen
        cvtColor(img, img, COLOR_RGB2BGR);
        imshow(kWindowName, img);

        // Exit script
        if (waitKey(1) >= 0)
        {
            break;
        };

        // Handle FPS counter
        frame_counter++;
        loop_dt = duration_cast<milliseconds>(high_resolution_clock::now() - loop_start).count();
        loop_start = high_resolution_clock::now();
        time_since_fps_update += loop_dt;
        if (time_since_fps_update > kUpdateFpsMilliseconds)
        {
            fps = (double)frame_counter / (double)time_since_fps_update * 1000;
            time_since_fps_update -= kUpdateFpsMilliseconds;
            frame_counter = 0;
        }
    }

    destroyWindow(kWindowName);
    return 0;
}

I think that it might have something to do with the color scalars or with the products vector.

Any help is appreciated!


Solution

  • The main culprit is likely:

            for (int i = 0; i <= products.size(); i++)
            {
                img = products[i].update(speed, img, loop_dt);
            }
    

    That should be < products.size(). Vectors use indices from 0 to size()-1, like pretty much all other std containers.