I am using VC++ 2010 Express. I’ve written the following routine:
HRESULT DLTimeStampNow::Receive(IMediaSample *pSample)
{
long long fnum, fnum2;
REFERENCE_TIME timeStart=0, timeEnd=10000;
// make this sample available for the next worker-thread call to Transform()
pSample->AddRef();
CRefTime rtStream;
timeStart = 0;
timeEnd = 10000;
pSample->SetTime(&timeStart, &timeEnd);
EnterCriticalSection(&cs);
if (pSampleWaiting != NULL)
((IMediaSample*)pSampleWaiting)->Release();
pSampleWaiting = pSample;
SetEvent(hSampleIsReady);
LeaveCriticalSection(&cs);
return S_OK;
}
This is part of a work in progress, which is why variables like fnum and rtStream are present but unused.
At run-time, the VC++ debugger refuses to put breakpoints at timeStart = 0 or timeEnd = 10000, which would be fine except it also fails to initialize them. The debugger shows they contain garbage at the point when execution reaches pSample->SetTime(&timeStart, &timeEnd);. To cope with this, I have added =0 and =10000 to the variable declarations. This works, but why is it necessary? I am guessing the compiler is doing some kind of optimization where it avoids the constant assignments in the code, but it is also forgetting(?) to ever actually assign values to timeStart and timeEnd (unless I initialize them in their declaration statement, as above).
What gives?
Compiler optimization could do various things to cause this behavior, such as storing local variables in registers or turning single-use variables into constants. If you want to understand what’s happening, one approach is to use assembly view/mode in the debugger to see what happens before and after the function call to
SetTime.