释放内存时崩溃

发布于 2024-12-26 07:23:08 字数 824 浏览 2 评论 0原文

我有一个用于修改pdf文件内容的应用程序。完成所需的更改后,我保存文件并退出应用程序。在此过程中,我想清除分配给 pdf 文件的内存。代码如下:

    for (i32 i = 0; i < job_state->PDF_IList_len; i++) {
        if(IList[i].PIL_ImageData.PDH_DataPtr.PDH_PathName)
            free (IList[i].PIL_ImageData.PDH_DataPtr.PDH_PathName);
        if(IList[i].PIL_DependentImages)
            free (IList[i].PIL_DependentImages);
    }
    job_state->PDF_ImageList = NULL;
    job_state->PDF_context = NULL;
    free (IList);

job_struct是一个保存文件信息的数据结构。 PDH_Pathname 是存储 pdf 详细信息的临时目录。

我的应用程序总是崩溃,

free (IList[i].PIL_ImageData.PDH_DataPtr.PDH_PathName);

我注意到每当它崩溃时,就无法评估 free (IList[i].PIL_ImageData.PDH_DataPtr.PDH_PathName) 的值(这就是调试器所说的)。

如果 for 循环被注释掉,则不会观察到崩溃。请让我知道可能是什么问题。

I have an application which is used to modify content of a pdf file. Once the required changes are made , i save the file and quit the application. During this process i would like the clear the memory that has been assigned to the pdf file. The code is as given below:

    for (i32 i = 0; i < job_state->PDF_IList_len; i++) {
        if(IList[i].PIL_ImageData.PDH_DataPtr.PDH_PathName)
            free (IList[i].PIL_ImageData.PDH_DataPtr.PDH_PathName);
        if(IList[i].PIL_DependentImages)
            free (IList[i].PIL_DependentImages);
    }
    job_state->PDF_ImageList = NULL;
    job_state->PDF_context = NULL;
    free (IList);

job_structure is a data structure to save info about the file.
PDH_Pathname is the temp directory where the details of the pdf is stored.

My application always crashes at

free (IList[i].PIL_ImageData.PDH_DataPtr.PDH_PathName);

I have noticed that whenever it crashes,the value for free (IList[i].PIL_ImageData.PDH_DataPtr.PDH_PathName) cannot be evaluated(thats what the debugger says).

If the for loop is commented out, there is no crash observed. Please let me know what can be the issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小傻瓜 2025-01-02 07:23:09

鉴于您所显示的代码,无法说出根本原因,因为:

  • 您的代码没有显示如何在错误指针上完成分配。
  • 它也没有显示所有(可能是很多地方)所述指针的使用位置。

第二个有点棘手,因为指针可能已传递到 n 个控制路径,在这些路径中它可能会再次被释放或被损坏,可以理解的是,不可能在这里提供所有这些作为一部分问题的。

鉴于上述情况,您最好的选择是使用内存分析工具,例如 Unix/Linux 系统上的 Valgrind 或 Windows 上的 Rational Purify。一旦您使用这些运行您的应用程序,它们将准确地指出问题的根本原因。

Given the code that you have shown it is not possible to say anything about the root cause because:

  • Your code does not show how the allocation was done on the faulting pointer.
  • Nor does it show where all(could be lot many places) the said pointer was used.

The second one is a bit tricky because the pointer could have been passed to n control paths where it could be freed again or gets corrupted and understandably it is impossible to provide all of that in here as a part of the Question.

Given the above your best bet is to use a memory profiling tool like Valgrind on Unix/Linux systems or Rational Purify On Windows. Once you run your application with these, they will exactly point you out the root cause of the issue.

云之铃。 2025-01-02 07:23:09

除了 Als 使用 valgrind 的答案之外,在释放指针后,将指针设置为 NULL 可能是值得的:

for (i32 i = 0; i < job_state->PDF_IList_len; i++) {
    free (IList[i].PIL_ImageData.PDH_DataPtr.PDH_PathName);
    IList[i].PIL_ImageData.PDH_DataPtr.PDH_PathName = NULL;
    free (IList[i].PIL_DependentImages);
    IList[i].PIL_ImageData.PDH_DataPtr.PDH_PathName = NULL;        
}

free() 不会将指针设置为 >NULL,它只释放存储在那里的内存。由于您正在测试指针的值是否为零以确定是否需要释放它,因此如果此代码被调用两次(或者如果有任何情况),则不将指针设置为 NULL 可能会导致双重释放IList[] 内部指向稍后在 IList[] 中也会遇到的结构)。

根据评论,我还删除了 free 之前对 NULL 的检查(正如 Joshua Green 指出的,free(NULL) 是完全安全的)。

In addition to Als' answer to use valgrind, it's probably worth setting your pointers to NULL after you've freed them:

for (i32 i = 0; i < job_state->PDF_IList_len; i++) {
    free (IList[i].PIL_ImageData.PDH_DataPtr.PDH_PathName);
    IList[i].PIL_ImageData.PDH_DataPtr.PDH_PathName = NULL;
    free (IList[i].PIL_DependentImages);
    IList[i].PIL_ImageData.PDH_DataPtr.PDH_PathName = NULL;        
}

free() doesn't set pointers to NULL, it only releases the memory stored there. Since you're testing the value of the pointer against zero to determine whether it needs to be freed, not setting the pointers to NULL could be causing a double free if this code is called twice (or if anything inside IList[] points to a structure also encountered later in the IList[]).

As per a comment, I've also removed the check against NULL before the free (as Joshua Green points out, free(NULL) is perfectly safe).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文