简体   繁体   English

malloc问题和内存堆

[英]malloc problems and memory heap

i've been running into a weird problem with mallocing/freeing memory. 我已经遇到了与分配/释放内存的怪异问题。 i can't show the whole code itself so please make do with a very bare partial: 我无法显示整个代码本身,因此请使用一个非常裸露的部分:

#include <stdio.h>

int main(void)
{
     void *ptr;

     ptr = malloc(sizeof(node));

     /* making sure what the value is when it was initialized */
     printf("head_node: %p\n", ptr);


     /* i do a lot of things here, such as appending the node, sometimes truncating them */


     /* --------------------------------------------------------------------------------*/

     /* making sure what the value is still the same: */
     printf("head_node: %p\n", ptr);
     free(ptr);

     printf("done");
}

where node is declared somewhere else. 在其他地方声明节点的位置。

at the point where i free the ptr and it's initial value is the same right before freeing it and it's not NULL, i get the error dialog box in windows. 在我释放ptr的点上,它的初始值在释放它之前是相同的,并且它不是NULL,我在Windows中得到了错误对话框。

now here's is the odd part. 现在这是奇怪的部分。 when i compile it under MinGW and run it within MSYS (MinGW console shell) or outside of it, it doesn't run into any error. 当我在MinGW下编译它并在MSYS(MinGW控制台外壳程序)内或外部运行它时,它没有遇到任何错误。 when i build it under MS Visual Express 2012 and debug it under it, it doesn't run into any error. 当我在MS Visual Express 2012下构建它并对其进行调试时,它没有遇到任何错误。 but when i run the same program built by MS Visual Express outside MS Visual, i always get an error. 但是,当我运行由MS Visual Express在MS Visual之外构建的相同程序时,总是会出现错误。

what's even weirder is that it first prints out "done", which is at the VERY LAST point of the program before the error pops up. 更奇怪的是,它首先打印出“ done”(完成),该错误在出现错误之前位于程序的最后一点。 not very helpful when tracking the source of error... 跟踪错误源时不是很有帮助...

if there was any error that's being caught in the normal windows environment, then why not in MS Visual?? 如果在正常的Windows环境中捕获了任何错误,那为什么不在MS Visual中呢?

here's the exception message being displayed: 这是显示的异常消息:

File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgheap.c
Line: 1322
Expression: _CrtIsValidHeapPointer(pUserData)

Your symptoms point to you corrupting the heap somewhere in your missing code. 您的症状表明您破坏了丢失代码中某处的堆。 You are probably either writing to an invalid pointer or writing too much to a valid one somewhere. 您可能正在写一个无效的指针,或者在某个地方写了太多的有效指针。 The reason you only get the crash when you exit is the heap corruption is only noticed when the program is cleaning up, and different compilers do different amounts of checking in this phase. 仅在退出时崩溃的原因是只有在程序清理时才会注意到堆损坏,并且在此阶段,不同的编译器执行不同的检查量。

Is this your only malloc in the whole program? 这是您整个程序中唯一的malloc吗? If so, that is likely part of your problem -- your 'node' (aka head_node) implies you are doing something with a list. 如果是这样,那很可能是您的问题的一部分-您的“节点”(又称head_node)暗示您正在使用列表进行操作。 If you are accessing node->next (or whatever you are calling your linking pointer) without doing a malloc for that, then there is your error. 如果您在不执行malloc的情况下访问node-> next(或任何您正在调用的链接指针),那么就会出现错误。 without more of your code, it is impossible to say more. 没有更多的代码,就不可能说更多。

One other subtle thing that might be an issue, especially since the issue disappears with different build environment is you are not including stdlib.h. 另一个可能是问题的细微问题,尤其是由于在不同的构建环境下该问题消失时,您不包括stdlib.h。 It might be that you just left it out of your "minimal" example, but not having a prototype in scope for malloc() can cause some very strange issues. 可能是您只是将其排除在“最小”示例之外,但在malloc()范围内没有原型可能会导致一些非常奇怪的问题。

It may be a memory leak. 可能是内存泄漏。 Your pointer may be pointing to an invalid location.Its called a dangling pointer. 您的指针可能指向无效的位置,称为悬挂指针。 So check whether your pointer is pointing to a location which is valid(ur pointer may also point to an invalid junk location) 因此,请检查您的指针是否指向有效的位置(您的指针也可能指向无效的垃圾位置)

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

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