2

Could someone please help me to solve unhandled exception error when using visual C++ 2008? the error is displayed as follow: Unhandled exception at 0x00411690 in time.exe: 0xC0000005: Access violation reading location 0x00000008

Some details:
- tm 0x00000000 {tm_sec=??? tm_min=??? tm_hour=??? ...} tm *
tm_sec CXX0030: Error: expression cannot be evaluated
...

Actually when I used visual c++ 6 in the past, there weren't any error and the program was running fine. But now ehen I use visual 2008, I am getting this Unhandled exception error.

Here is the program:

...
int gettimeofday(struct timeval *tv, struct timezone *tz)
{
  FILETIME ft;
  unsigned __int64 tmpres = 0;
  static int tzflag = 0;

  if (NULL != tv)
  {
    GetSystemTimeAsFileTime(&ft);

    tmpres |= ft.dwHighDateTime;
    tmpres <<= 32;
    tmpres |= ft.dwLowDateTime;

    tmpres /= 10;  /*convert into microseconds*/
    /*converting file time to unix epoch*/
    tmpres -= DELTA_EPOCH_IN_MICROSECS; 
    tv->tv_sec = (long)(tmpres / 1000000UL);
    tv->tv_usec = (long)(tmpres % 1000000UL);
  }

  if (NULL != tz)
  {
    if (!tzflag)
    {
      _tzset();
      tzflag++;
    }
    tz->tz_minuteswest = _timezone / 60;
    tz->tz_dsttime = _daylight;
  }

  return 0;
}


uint32_t stampstart()
{
 struct timeval  tv;
 struct timezone tz;
 struct tm      *tm;
 uint32_t         start;

 gettimeofday(&tv, &tz);
 tm = localtime(&tv.tv_sec);

 printf("TIMESTAMP-START\t  %d:%02d:%02d:%d (~%d ms)\n", tm->tm_hour,
        tm->tm_min, tm->tm_sec, tv.tv_usec,
        tm->tm_hour * 3600 * 1000 + tm->tm_min * 60 * 1000 +
        tm->tm_sec * 1000 + tv.tv_usec / 1000);   /////---debugger stops here---

 start = tm->tm_hour * 3600 * 1000 + tm->tm_min * 60 * 1000 +
  tm->tm_sec * 1000 + tv.tv_usec / 1000;

 return (start);

}

thanks for your replies:

make
  • 755
  • 7
  • 21
  • 37
  • Use the Visual Studio debugger, step into the code, and tell us where the exception happens... – Inverse Mar 22 '10 at 07:01
  • On which line you're getting this exception...? – Mihir Mehta Mar 22 '10 at 07:04
  • Thanks for replies ... see the line : tm->tm_sec * 1000 + tv.tv_usec / 1000); of uint32_t stampstart() function – make Mar 22 '10 at 07:11
  • the problem is that the variable " tv.tv_sec " returns a negative value. why? is ther any idea? thanks – make Mar 23 '10 at 02:26
  • have a look here: http://stackoverflow.com/questions/2494356/how-to-use-gettimeofday-or-something-equivalent-with-visual-studio-c-2008 –  Dec 20 '12 at 17:01

6 Answers6

2

Try Something like...

 tm = localtime(&tv.tv_sec);
if(tm)
{
 printf("TIMESTAMP-START\t  %d:%02d:%02d:%d (~%d ms)\n", tm->tm_hour,
        tm->tm_min, tm->tm_sec, tv.tv_usec,
        tm->tm_hour * 3600 * 1000 + tm->tm_min * 60 * 1000 +
        tm->tm_sec * 1000 + tv.tv_usec / 1000); 

 start = tm->tm_hour * 3600 * 1000 + tm->tm_min * 60 * 1000 +
  tm->tm_sec * 1000 + tv.tv_usec / 1000;
}
else
{
 // failed to retrive local time
}
Mihir Mehta
  • 13,743
  • 3
  • 64
  • 88
  • thanks for the idea. yes! it fails to retrieve local time. Is there idea on how to solve this problem? thanks again for your help – make Mar 22 '10 at 07:40
  • you can see errno for exact problem...if localtime() failed it will set errno ... – Mihir Mehta Mar 22 '10 at 09:13
  • yes! it is localtime as it doesn't returns anythink. actually i don't understand why it works with VC++6 and not with VC++2008 ...thanks! – make Mar 22 '10 at 14:11
1

The access violation refers address 0x00000008, which probably means that your code is accessing a field at offset 8 in a struct to which you point with a NULL pointer. I guess that localtime returns a NULL pointer. Check this.

Patrick
  • 23,217
  • 12
  • 67
  • 130
  • Probably, but why it works with VC++6 and not with VC++2008? thanks for your reply! – make Mar 22 '10 at 07:43
1

"Access violation reading location 0x00000008"

An address that low is probably caused by accessing a null pointer. In this case, tm is probably 0 because of a problem with localtime or gettimeofday. You get address 0x08 instead of 0x00 because the compiler is trying to read a value 8 bytes into the struct.

Dennis Zickefoose
  • 10,791
  • 3
  • 29
  • 38
  • I think this is the cause as well, but I need to read the time ... thanks for reply! – make Mar 22 '10 at 07:41
1

The type of member tv.tv_sec is long, but localtime expects a time_t * parameter. In VC6 this worked because both long and time_t were 32-bit, but in VS2008 time_t is a 64-bit type so they are incompatible.

This should fix it:

//add to beginning of stampstart function:
time_t t;

//... other code...

//put this instead of call to localtime:
t = tv.tv_sec;
tm = localtime(&t);
interjay
  • 107,303
  • 21
  • 270
  • 254
  • Actually i already tried this : tm = localtime((time_t *) &tv.tv_sec); and still getting the same error (exception handling)... – make Mar 22 '10 at 19:30
  • @make: I forgot this is C, so the variables have to be declared at the beginning of the function. I edited my answer to show this. As for your second comment, casting it to `time_t*` won't help because it doesn't have an actual `time_t` to point to. – interjay Mar 22 '10 at 19:59
  • thanks! yes you are right as I remenbered just after i replied to your message that it is C. yes! i tried this also and still getting the same error ... any other ideas... thanks again for your help – make Mar 22 '10 at 20:03
  • @make: By "same error" do you mean C2275 or access violation? – interjay Mar 22 '10 at 20:05
  • I mean the unhandled exception... here is a copy : First-chance exception at 0x0041166a in tim.exe: 0xC0000005: Access violation reading location 0x00000008. Unhandled exception at 0x0041166a in tim.exe: 0xC0000005: Access violation reading location 0x00000008. – make Mar 22 '10 at 20:09
0

With that address, my SWAG is that tm is NULL. Since you never check for the NULL after the call to localtime(), you are dereferencing a NULL pointer.

Edit: SWAG = Scientific Wild Ass Guess.

The address 0x00000008 is the same offset of tm.tm_hour. You are trying to access tm->tm_hour when you pass in the parameter to printf().

Ants
  • 2,628
  • 22
  • 35
  • which swag are you talking about ...? I get error when I run the executable. the program is compiled and linked without errors. but when I execute it, I get unhandled exception ... thanks for reply! – make Mar 22 '10 at 07:28
  • "Specific while ass guess" :) And it would compile cleanly, because the compiler has no way of knowing that `tm` is going to end up null when you run the program. That can't happen until you execute. – Dennis Zickefoose Mar 22 '10 at 07:35
  • thanks for reply. actually I don't have experience with visual C++ and it is the first time I heared about SWAG.So please explain and tell me how to use to solve this poblem ... thanks again – make Mar 22 '10 at 14:09
0

To catch this kind of exception a minor project tweaking is needed. Just enable /EHa option in project settings. See Project Properties -> C/C++ -> Code Generation -> Modify the Enable C++ Exceptions to "Yes With SEH Exceptions". That's it!

See details here: http://msdn.microsoft.com/en-us/library/1deeycx5(v=vs.80).aspx

Volodymyr Frytskyy
  • 1,233
  • 11
  • 15