It appears that re-initialization of encoding session with NVIDIA Video Codec SDK is or might be producing an unexpected memory leak.
So, how does it work exactly?
NVENCSTATUS Status; Status = m_ApiFunctionList.nvEncInitializeEncoder(m_Encoder, &InitializeParams); assert(Status == NV_ENC_SUCCESS); // NOTE: Another nvEncInitializeEncoder call Status = m_ApiFunctionList.nvEncInitializeEncoder(m_Encoder, &InitializeParams); assert(Status == NV_ENC_SUCCESS); // Still success ... Status = m_ApiFunctionList.nvEncDestroyEncoder(m_Encoder); assert(Status == NV_ENC_SUCCESS);
The root case problem is secondary
nvEncInitializeEncoder call. Alright, it might be not exactly how API is designed to work, but returned statuses all indicate success, so it will be a bit hard to justify the leak by telling that second initialization call was not expected in first place. Apparently the implementation overwrites internally allocated resources without accurate releasing or reusing. And without triggering any warning of sorts.
Another part of the problem is eclectic design of the API in first place. You open a “session” and obtain “encoder” as a result. Then you initialize “encoder” and when you are finished you destroy “encoder”. Do you destroy “session”? Oh no, you don’t have any session at all except that API opening “session” actually opens an “encoder”.
So when I get into situation where I want to initialize encoder and it is already initialized then what I do is to destroy existing “encoder”, open new “session” and now I can initialize the session-encoder once again with the initialization parameters.