释放内存时崩溃
我有一个用于修改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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
鉴于您所显示的代码,无法说出根本原因,因为:
第二个有点棘手,因为指针可能已传递到 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:
The second one is a bit tricky because the pointer could have been passed to n control paths where it could be
free
d 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.
除了 Als 使用 valgrind 的答案之外,在释放指针后,将指针设置为 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:free()
doesn't set pointers toNULL
, 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 toNULL
could be causing a double free if this code is called twice (or if anything insideIList[]
points to a structure also encountered later in theIList[]
).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).