Question

I have been trying to write a small application which creates videos for you and you can send across your own images. For encoding the video I have used Media foundation library.

Everything works fine. I am able to create a very short video, however once I try to create a big video my application starts taking lots of memory and eventually starve for memory and crashes. Memory goes in the range of 1.5 GBs.

I have done some debugging found out that this method is leaking memory. After trying lots of delete and free memory combinations I am not able to figure out the problem. Could you please help me with it. This is the method which adds the frame.

 HRESULT VideoCompressor::addFrame(BYTE* bitmapBytes)
{
    const long imageBufferWidth = VIDEO_WIDTH * 4;
    const DWORD bufferLength = imageBufferWidth * VIDEO_HEIGHT;
    IMFSample *sample = NULL;
    BYTE *dataBuffer = NULL;
    IMFMediaBuffer *sampleBuffer = NULL;
    DWORD maxLength;
    //std::shared_ptr<BYTE> *dataBuffer = NULL;

    if (!SUCCEEDED(MFCreateMemoryBuffer(bufferLength, &sampleBuffer)))
        return E_FAIL;

    sampleBuffer->GetMaxLength(&maxLength);
    if (!SUCCEEDED(sampleBuffer->Lock(&dataBuffer, NULL, NULL)))
        return E_FAIL;

    if (MFCopyImage(dataBuffer, imageBufferWidth,
        (BYTE *) bitmapBytes, imageBufferWidth, 
        imageBufferWidth, VIDEO_HEIGHT) 
        != S_OK)
        return E_FAIL;

    sampleBuffer->Unlock();
    sampleBuffer->SetCurrentLength(bufferLength);

    if (!SUCCEEDED(MFCreateSample(&sample)))
        return E_FAIL;

    if (!(SUCCEEDED(sample->AddBuffer(sampleBuffer))))
        return E_FAIL;


    if(!(SUCCEEDED(sample->SetSampleTime(startTime))))
        return E_FAIL;

    if(!(SUCCEEDED(sample->SetSampleDuration(durationTime))))
        return E_FAIL;

    if(!(SUCCEEDED(writer->WriteSample(/*streamIndex*/ 0, sample))))
        return E_FAIL;

    startTime = startTime  + durationTime;

    //sample->Release();
    //sampleBuffer->Release();

    SafeRelease(&sample);
    SafeRelease(&sampleBuffer);

    return S_OK;
}

Implementation of SafeRelease is

template <class T> void SafeRelease(T **_object)
{
    if (*_object)
    {
        (*_object)->Release();
        *_object = NULL;
    }
}

I am suspecting that it has to do with BYTE *dataBuffer . I tried deleting it using the delete [] keyword but it wasn't of any use.

Was it helpful?

Solution

Answered by @jeff

If your code is executing through the entire function and returning S_OK, then I don't see it, and I don't believe it is in this function. However you should fix the fact that you have multiple exit points without calling SafeRelease(..).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top