I use Sample Grabber Sink in my Media session using most of code from msdn sample.

In OnProcessSample method I memcpy data to media buffer, attach it to MFSample and put this one into main process pointer. Problem is I either get memory leaking or crashes in ntdll.dll

ntdll.dll!@RtlpLowFragHeapFree@8() Unknown

SampleGrabberSink:

OnProcessSample(...)
{
    MFCreateMemoryBuffer(dwSampleSize,&tmpBuff);
    tmpBuff->Lock(&data,NULL,NULL);
    memcpy(data,pSampleBuffer,dwSampleSize); tmpBuff->Unlock();
    MFCreateSample(&tmpSample);
    tmpSample->AddBuffer(tmpBuff);

    while(!(*Free) && (*pSample)!=NULL)
    {
        Sleep(1);
    }

    (*Free)=false;
    (*pSample)=tmpSample;
    (*Free)=true;
    SafeRelease(&tmpBuff);
}

in main thread

ReadSample()
{
    if(pSample==NULL)
        return;
    while(!Free)
        Sleep(1);
    Free=false;
    //process sample into dx surface//
    SafeRelease(&pSample);
    Free=true;
}

//hr checks omitted// With this code i get that ntdll.dll error after playing few vids. I also tried to push samples in qeue so OnProcess doesn't have to wait but then some memory havent free after video ended. (even now it practicaly doesn't wait, Session rate is 1 and main process can read more than 60fps)

EDIT: It was thread synchronization problem. Solved by using critical section thanks to Roman R.

有帮助吗?

解决方案

It is not easy to see is from the code snippet, but I suppose you are burning cycles on a streaming thread (you have your callback called on) until a global/shared variable is NULL and then you duplicate a media sample there.

You need to look at synchronization APIs and serialize access to shared variables. You don't do that and eventually either you are accessing freed memory or breaking reference count of COM object.

You need an event set externally when you are ready to accept new buffer from the callback, then the callback sees the event, enters critical section (or, reader/writer lock), does your *pSample magic there, exits from critical section and sets another event indicating availability of a buffer.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top