繁体   English   中英

调试glibc free():无效的指针

[英]Debug glibc free(): invalid pointer

我正在尝试调试最终抛出的代码

*** glibc detected *** ./build/smonitor: free(): invalid pointer:

它的挑战性在于我不使用免费软件...我看过其他SO帖子,其中有复制该问题的示例..我需要有关如何调试的帮助。 首先,我是C / C ++ n00b,所以我的指针技能正在开发中,但是我并没有做太多的动态内存分配(我认为)。

我开始编写自己的“安全”应用程序,从相机中获取快照并将其写入NFS共享,最终将显示每个相机的快照。 现在,我从1台摄像机拍摄并在显示窗口中循环(使用opencv)。 在获得核心转储之前,我可以运行一段时间(〜小时)。 我创建了一个运行窗口的线程,我应该循环运行,直到将运行标志设置为false,然后再调用cvReleaseImage。。我不知道为什么会失败,因此非常感谢任何指导!

// will be replaced with camera X filename on NFS share
std::string generate_filename() 
{
    static const char alphanum[] =
                "0123456789"
                "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                "abcdefghijklmnopqrstuvwxyz";

    std::string filename = "";
    std::stringstream ss;
    for (int i = 0; i < 10; i++)
    {
        ss << alphanum[rand() % (sizeof(alphanum) - 1)];
    }
    ss << ".jpg";
    printf("Generated filename: %s\n", ss.str().c_str());
    return ss.str();
}

std::string generate_file_path()
{
    std::stringstream ss;

    ss << CAPTURES_PATH << generate_filename();
    return ss.str();
}


void capture_photo(std::string& filepath)
{
    time_t now;
    time_t end;
    double seconds;
    bool cancelCapture = false;
    int count = 0;

    CvCapture* capture = cvCreateCameraCapture(0);
    printf(“Opened camera capture\n");
    IplImage* frame;
    while(1)
    {
        frame = cvQueryFrame(capture);
        if (!frame)
        {
            fprintf(stderr, "Could not read frame from video stream\n\n");
        } else
        {
            cvShowImage(WINDOW, frame);
            cvWaitKey(100);
            if (get_snapshot_enabled()) // simulate delay between snapshots
            {
                filepath = generate_file_path();
                printf("Saving image\n");
                cvSaveImage(filepath.c_str(), frame);
                break;
            }
        }
    }
    printf("Ending camera capture\n");
    cvReleaseCapture(&capture);
}

void* manage_window(void* arg)
{
    time_t now;
    time_t end;
    double seconds = 0;
    double stateSec;

    int i = 0;
    int rem = 0;

    IplImage* images[10];

    time_t lastUpdate;
    time_t tDiff; // time diff

    cvNamedWindow(WINDOW, CV_WINDOW_FREERATIO);
    cvSetWindowProperty(WINDOW, CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);

    std::string filepath;
    time(&now);
    int lastPos = 0;
    while (1)
    {
        if (get_snapshot_enabled())
        {
            write_console_log("Going to capture photo\n");
            // camera was selected
            filepath = generate_file_path();
            printf("Generated filepath: %s\n", filepath.c_str());
            capture_photo(filepath);

            if (!filepath.empty())
            {
                printf("Received filepath: %s\n", filepath.c_str());
                time(&now);

                images[lastPos] = cvLoadImage(filepath.c_str());
                cvShowImage(WINDOW, images[lastPos]);
                cvWaitKey(100);
                if (lastPos == 10) lastPos = 0;
                else lastPos++;
            } 
        }

        time(&end);
        seconds = difftime(end, now);
        if (seconds >= 5)
        {
            cvShowImage(WINDOW, images[ i % 10])
            cvWaitKey(100);

            i++;
            time(&now);
        }

        // check if we're running
        if (!get_running())
        {
            // log some error we're not running...
            write_logs("Window thread exiting, not running...");
            break;
        }
    }

    for (i=0; i < 10; i++)
        cvReleaseImage(&images[i]);

    pthread_exit(NULL);
}

void* manage_window(void* arg)void* manage_window(void* arg)

IplImage* images[10];

images[lastPos] = cvLoadImage(filepath.c_str());

if (lastPos == 10) lastPos = 0;
else lastPos++;

其中lastPos可以增加到10,导致

images[10] = cvLoadImage(filepath.c_str());

即无效写入超出数组末尾。 我认为类似valgrind的东西会检测到这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM