-
September 6th, 2004, 09:35 AM
#1
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.
-
September 6th, 2004, 10:11 AM
#2
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.
-
September 6th, 2004, 02:37 PM
#3
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
-
September 7th, 2004, 02:11 AM
#4
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.
-
July 2nd, 2011, 03:53 AM
#5
Re: QueryPerformanceCounter failure
Originally Posted by cilu
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.
-
September 12th, 2013, 07:11 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|