【问题标题】:Debug glibc free(): invalid pointer调试 glibc free(): 无效指针
【发布时间】:2016-05-06 22:44:32
【问题描述】:

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

*** 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);
}

【问题讨论】:

  • 如果您使用的是C++,请不要标记C
  • 您是否在开发环境的调试器下运行程序?应该可以帮助您缩小出现错误free 的范围。
  • 这个if (lastPos == 10) lastPos = 0; else lastPos++ 似乎导致images[10] = ... 即超出数组末尾的无效写入。
  • JJ 是对的,你可能真的想在检查 1 结束之前增加:if (++lastPos == 10) lastPos = 0;(没有任何else...
  • 哦,伙计@J.J.Hakala,你看中了,那个从我身边溜走了……为什么应用程序没有早点崩溃?

标签: c++ glibc coredump invalid-pointer


【解决方案1】:

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 这样的东西会检测到这一点。

【讨论】:

  • 在尝试 Valgrind 之前,请先尝试一下 Address Sanitizer。它运行得更快,并捕获了大多数相同的错误。当然,Valgrind 仍然是这类事情的黄金标准。
猜你喜欢
  • 2015-03-31
  • 2013-08-01
  • 2012-05-04
  • 1970-01-01
  • 1970-01-01
  • 2016-06-16
  • 1970-01-01
  • 2016-03-27
  • 2016-03-28
相关资源
最近更新 更多