I see that this question‘s answer is really popular regarding how to print a cv::Mat image into an MFC application.
However, I was wondering if there is a memory leak there? or if there is none, how is that possible?
In specific, I’m wondering about the memset(bmih, 0, sizeof(*bmih)) part. Is it that MFC somehow manages the memory here? Can someone provide some information regarding this?
void COpenCVTestView::FillBitmapInfo(BITMAPINFO* bmi, int width, int height, int bpp, int origin)
{
assert(bmi && width >= 0 && height >= 0 && (bpp == 8 || bpp == 24 || bpp == 32));
BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);
// this part shouldn't leak?
memset(bmih, 0, sizeof(*bmih));
bmih->biSize = sizeof(BITMAPINFOHEADER);
bmih->biWidth = width;
bmih->biHeight = origin ? abs(height) : -abs(height);
bmih->biPlanes = 1;
bmih->biBitCount = (unsigned short)bpp;
bmih->biCompression = BI_RGB;
if (bpp == 8)
{
RGBQUAD* palette = bmi->bmiColors;
for (int i = 0; i < 256; i++)
{
palette[i].rgbBlue = palette[i].rgbGreen = palette[i].rgbRed = (BYTE)i;
palette[i].rgbReserved = 0;
}
}
}
Where do you see a memory leak? There’s no dynamic allocation, and no
pointer manipulation. A
memsetcoul cause a memory leak, if itoverwrote a pointer to dynamically allocated memory, but there’s no
pointer in a
BITMAPINFOHEADER, just integers.