CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    QueryPerformanceCounter failure

    Hi,

    I'm getting an error while calling QueryPerformanceCounter and I don't know why.
    Code:
    LARGE_INTEGER start;
    BOOL bRet = QueryPerformanceCounter(&start);
    
    ASSERT(bRet);
    if(FALSE == bRet)
    {
       int nError = GetLastError();
       PrintError(nError);
    }
    The function returns FALSE, meaning it failed, but when I call GetLastError() it returns 0, meaning ERROR_SUCCES; there was no error...

    However If I do this, it works just fine:
    Code:
    LARGE_INTEGER* start = new LARGE_INTEGER;
    BOOL bRet = QueryPerformanceCounter(start);
    
    ASSERT(bRet);
    if(FALSE == bRet)
    {
       int nError = GetLastError();
       PrintError(nError);
    }
    
    delete start;
    LARGE_INTEGER looks like this:
    Code:
    typedef union union 
    {  
      struct 
      {    
         DWORD LowPart;    
         LONG HighPart;  
      };  
       LONGLONG QuadPart;
    } LARGE_INTEGER, *PLARGE_INTEGER;
    I'm working on WinXP, with VC6.0, SP6, and have an AthlonXP 1600+.

    Why is the first code failing (with 0 returned by GetErrorSucces) and the second one is just ok?

    Thanks.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: QueryPerformanceCounter failure

    I forgot to mention that I'm doing this in a multi-thread MDI application. I cannot post it here, it has more than 200,000 code lines and it ain't worth to isolate just that part.

    I tried to create another simple (MDI) application, put there the code and it works beautiful. So, it makes me wonder even more...
    Last edited by cilu; September 6th, 2004 at 10:13 AM.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Apr 2002
    Location
    St.Petersburg, Russia
    Posts
    714

    Re: QueryPerformanceCounter failure

    cilu, are you sure that problem is in multithreading? AFAIR QueryPerformanceCounter() is thread-safe function, so may be there are another circumstances?
    With best wishes, Alexander Hritonenkov

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: QueryPerformanceCounter failure

    I didn't say that the problem is multi-threading, I just wanted to give as much details as possible. The code posted is taken from a member function of a CDocument derived class.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Jun 2011
    Location
    Antony (near Paris), France
    Posts
    11

    Re: QueryPerformanceCounter failure

    Quote Originally Posted by cilu View Post
    Hi,

    I'm getting an error while calling QueryPerformanceCounter and I don't know why.
    Code:
    LARGE_INTEGER start;
    BOOL bRet = QueryPerformanceCounter(&start);
    
    ASSERT(bRet);
    if(FALSE == bRet)
    {
       int nError = GetLastError();
       PrintError(nError);
    }
    The function returns FALSE, meaning it failed, but when I call GetLastError() it returns 0, meaning ERROR_SUCCES; there was no error...
    I just had the same problem but I notice something else:
    - If the variable is a member of the class: The function fails.
    - If the variable is a pointer (allocated with a new): The function works.
    - If the variable is local (on the stack): The function works.

    So my workaround is the following:

    Code:
    LARGE_INTEGER start;
    BOOL bRet = QueryPerformanceCounter(&start);
    
    ASSERT(bRet);
    if(FALSE == bRet)
    {
       int nError = GetLastError();
       PrintError(nError);
    }
    else
    {
       m_start = start;
    }
    Thanks for your post, it helps me to find a solution.

    Regards.

  6. #6
    Join Date
    Sep 2013
    Posts
    1

    Re: QueryPerformanceCounter failure

    For what it's worth it looks like QueryPerformanceCounter is sensitive to the argument alignment. specifically the structure needs to be 4 bytes aligned on my hardware.
    I stepped onto the similar problem while working with byte aligned data (pragma pack 1). Make sure you arguments are at least 4 bytes aligned.

    Cheers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured