Reputation: 277
I have next code:
void matToAvPicture(cv::Mat frame, AVPicture *picture)
{
cv::resize(frame, frame, cv::Size(m_streamWidth, m_streamHeight));
uint8_t* buf = (uint8_t*)malloc(avpicture_get_size(AV_PIX_FMT_BGR24, m_streamWidth, m_streamHeight)); // 3 bytes per pixel
for(int i = 0; i < m_streamHeight; i++)
{
memcpy(&(buf[i * m_streamWidth * 3]), &(frame.data[i * frame.step]), m_streamWidth * 3);
}
AVPicture bgrPicture;
avpicture_alloc(&bgrPicture, AV_PIX_FMT_BGR24, m_streamWidth, m_streamHeight);
avpicture_fill(&bgrPicture, buf, AV_PIX_FMT_BGR24, m_streamWidth, m_streamHeight);
sws_scale(rgbToYuvContext, (const uint8_t * const *)bgrPicture.data, bgrPicture.linesize, 0, m_streamHeight, picture->data, picture->linesize);
avpicture_free(&bgrPicture);
// free(buf);
}
But call of avpicture_free(&bgrPicture)
doesn't free memory completely. If I commented line avpicture_fill(&bgrPicture, buf, AV_PIX_FMT_BGR24, m_streamWidth, m_streamHeight);
and uncommented line
free(buf)
memory leak doesn't appear.
What is the problem?
Upvotes: 0
Views: 491
Reputation: 11174
uint8_t* buf = (uint8_t*)malloc(avpicture_get_size(AV_PIX_FMT_BGR24, m_streamWidth, m_streamHeight)); // 3 bytes per pixel avpicture_alloc(&bgrPicture, AV_PIX_FMT_BGR24, m_streamWidth, m_streamHeight); avpicture_fill(&bgrPicture, buf, AV_PIX_FMT_BGR24, m_streamWidth, m_streamHeight);
That's doing the same thing twice. avpicture_alloc allocates and fills the data array in a picture with a self-allocated buffer (free'ed with avpicture_free), and avpicture_fill fills the data structure with some buffer that you control yourself (in your case: malloc/free manually). Use either malloc+avpicture_fill+free, or use avpicture_alloc+avpicture_free. Don't use both.
Upvotes: 1