当前位置:  开发笔记 > 编程语言 > 正文

使用time.h时的Segfault

如何解决《使用time.h时的Segfault》经验,为你挑选了1个好方法。

好吧,我一直在尝试我所知道的所有事情,让这个程序停止崩溃,但我不明白为什么.我能够将问题与ctime代码隔离开来,并且只是做了一个小程序来演示出错了什么.此代码编译没有问题.

#include
#include

int main();
time_t getDay(time_t t);
int diffDay(time_t end,time_t begin);

int main()
{
    time_t curTime=time(NULL);  //Assign current time
    time_t curDay=getDay(curTime);  //Assign beginning of day
    time_t yesterday=curDay-16*60*60;   //Assign a time that's within yesterday
    time_t dif=diffDay(curTime,yesterday); //Assign how many days are between yesterday and curTime


    std::cout << "Cur Time: " << curTime << '\n'
            << "Cur Day: " << curDay << '\n'
            << "Yes Day: " << dif << '\n' << std::flush;

    char a;
    std::cin >> a; ///Program crashes after here.

    return 0;
}

///Get beginning of day that t is a part of
time_t getDay(time_t t)
{
    //Get current time
    struct tm* loctim=localtime(&t);

    if(loctim==0)
        return 0;

    //Set loctim to beginning of day
    loctim->tm_sec=0;
    loctim->tm_min=0;
    loctim->tm_hour=0;

    //Create a int from the new time
    int reval=mktime(loctim);

    //Free memory
    delete loctim;

    return reval;
}

///Calculate how many days are between begin and end
int diffDay(time_t end,time_t begin)
{
    time_t eDay=getDay(end); //Get beginning of day end is a part of
    time_t bDay=getDay(begin); //Get beginning of day begin is a part of
    time_t dif=(eDay-bDay)/(24*60*60); //Get how many days (86400 seconds)

    return dif;
}

这是我从调试中得到的一些文字.

调用堆栈
#0 77BC3242 ntdll!LdrLoadAlternateResourceModuleEx() (C:\Windows\system32\ntdll.dll:??)
#1 00000000 0x6d067ad3 in ??() (??:??)
#2 00000000 0x00000018 in ??() (??:??)
#3 77BC3080 ntdll!LdrLoadAlternateResourceModuleEx() (C:\Windows\system32\ntdll.dll:??)
#4 00000000 0x00000018 in ??() (??:??)
#5 77C60FCB ntdll!TpCheckTerminateWorker() (C:\Windows\system32\ntdll.dll:??)
#6 00000000 0x007f0000 in ??() (??:??)
#7 00000000 0x50000163 in ??() (??:??)
#8 00000000 0x00000018 in ??() (??:??)
#9 77C1AC4B ntdll!RtlReAllocateHeap() (C:\Windows\system32\ntdll.dll:??)
#10 00000000    0x007f0000 in ??() (??:??)
#11 00000000    0x50000163 in ??() (??:??)
#12 00000000    0x00000018 in ??() (??:??)
#13 77BC3080    ntdll!LdrLoadAlternateResourceModuleEx() (C:\Windows\system32\ntdll.dll:??)
#14 00000000    0x00000018 in ??() (??:??)
#15 769A9D45    msvcrt!malloc() (C:\Windows\syswow64\msvcrt.dll:??)
#16 769AF5D3    strcpy_s() (C:\Windows\syswow64\msvcrt.dll:??)
#17 769B2B18    open_osfhandle() (C:\Windows\syswow64\msvcrt.dll:??)
#18 00000000    0x00000018 in ??() (??:??)
#19 769B3C7D    msvcrt!_get_fmode() (C:\Windows\syswow64\msvcrt.dll:??)
#20 769BA6A0    msvcrt!_fsopen() (C:\Windows\syswow64\msvcrt.dll:??)
#21 00000000    0xc3458a06 in ??() (??:??)
#22 00000000    0x00000000 in ??() (??:??)

这也是来自同一版本的另一个调用堆栈.

#0 77BE708C ntdll!RtlTraceDatabaseLock() (C:\Windows\system32\ntdll.dll:??)
#1 00000000 0x6ccdaf66 in ??() (??:??)
#2 00000000 0x00000000 in ??() (??:??)

它是一些特殊的构建选项吗?我正在使用-std = c ++ 0x,但决定尝试没有它的程序,它仍然崩溃.感谢您的帮助,我一直在努力解决这个问题.



1> Marian..:

我认为问题在于:

  struct tm* loctim=localtime(&t);
  delete loctim;

localtime返回指向静态缓冲区的指针.你不应该释放它.这导致了"未定义的行为".即一些数据处于不一致状态,并可能导致程序的另一个地方崩溃,这似乎与问题没有直接关系.


你肯定不会用C++`delete`释放C函数的结果!
推荐阅读
雨天是最美
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有