[英]Does this leak memory? How can I prevent it?
I see that this question 's answer is really popular regarding how to print a cv::Mat image into an MFC application. 我看到关于如何将cv :: Mat图像打印到MFC应用程序中,这个问题的答案非常受欢迎。
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. 具体来说,我想知道
memset(bmih, 0, sizeof(*bmih))
部分。 Is it that MFC somehow manages the memory here? MFC是否以某种方式在这里管理内存? 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
memset
coul cause a memory leak, if it overwrote a pointer to dynamically allocated memory, but there's no pointer in a BITMAPINFOHEADER
, just integers. 如果
memset
coul重写了指向动态分配的内存的指针,但会导致内存泄漏,但是BITMAPINFOHEADER
没有指针,只有整数。
The memset
just fills the bmih
object with zeros in this case. 该
memset
只是填补了bmih
在这种情况下,零对象。 So there is no memory leak. 因此,没有内存泄漏。
The FillBitmapInfo
method is called with a pointer to a BITMAPINFO
object that was previously allocated somewhere else. 用指向先前已分配给其他位置的
BITMAPINFO
对象的指针调用FillBitmapInfo
方法。 bmih
is just a reference to the bmiHeader
member of the BITMAPINFO
struct. bmih
只是对BITMAPINFO
结构的bmiHeader
成员的引用。 memset
just assigns a value to the memory region referenced by bmih
. memset
只是为bmih
引用的内存区域分配一个值。
A memset()
doesn't cause a memory leak. memset()
不会导致内存泄漏。 That just writes 0x00 bytes into part of the memory pointed to by bmi
. 这只是将0x00字节写入
bmi
指向的部分内存。 There is not enough code here to determine whether any memory is leaked. 这里没有足够的代码来确定是否有任何内存泄漏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.