【问题标题】:fopen fails while debugging in eclipse cdt在 eclipse cdt 中调试时 fopen 失败
【发布时间】:2012-11-16 09:56:57
【问题描述】:

请看下面的代码sn-p:

#include <stdio.h>
int main ()
{
FILE * pFile;

pFile = fopen ("c:\\Temp\\test.txt","rb");
if (pFile!=NULL)
{
  printf("opened\n");
  fclose (pFile);
}
else{
  printf("error\n");
}
  return 0;
}

如果我编译这段代码 sn-p 并运行它,什么都不会失败。 如果我在调试模式下执行代码 fopen 失败并显示以下消息:

Thread [1] 0 (Suspended : Signal : SIGSEGV:Segmentation fault)  
  wtoi64() at 0x77b35eab    
    wtoi64() at 0x77b35a70  
    ntdll!RtlpSetUserPreferredUILanguages() at 0x77ba5eff    
    ntdll!KiRaiseUserExceptionDispatcher() at 0x77b6a3ba    
    toi64() at 0x77b35a70   
    msvcrt!malloc() at 0x775c9d45   
    strcpy_s() at 0x775cf5d3    
    open_osfhandle() at 0x775d2b18  
    0x18    
    0xbf39e545  
    <...more frames...

开发环境:

Windows 7
Eclipse CDT Juno Service Release 1
MINGW 4.7

编译器设置:

-O0 -g3 -Wall -c -fmessage-length=0

如果我启动调试器并逐步执行每条指令直到 fopen(..),是否有人知道为什么 fopen 会失败?

期待您的回答。提前致谢。

【问题讨论】:

    标签: windows eclipse debugging mingw eclipse-cdt


    【解决方案1】:

    我已经解决了这个问题。

    在 fopen 调用之前,我执行了以下代码:

    char* ptr = NULL;
    int size = 1024;
    ptr = malloc(sizeof(size));
    memset(ptr, 0, size);
    fopen("binary.txt", "rt");
    

    问题是,尽管只分配了 sizeof(size) 个字节,但 memset 被应用到了 1024 个字节上。 随后的 fopen 调用由于之前使用的 1024 字节的 memset 调用导致堆内存损坏而崩溃。

    到目前为止谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-31
      • 2011-06-21
      • 2011-04-26
      • 2013-10-19
      • 2011-12-22
      • 2011-04-25
      • 2013-12-23
      相关资源
      最近更新 更多