C++ 执行memcpy时内存泄漏
我正在尝试编写一个处理ffmpeg的实用程序。一旦我需要将图像平面从一个指针复制到另一个指针。从AVPicture结构到我自己的。 以下是一些来源 我自己的框架结构。在构造函数中分配的内存,在析构函数中解除分配的内存C++ 执行memcpy时内存泄漏,c++,memory-leaks,ffmpeg,C++,Memory Leaks,Ffmpeg,我正在尝试编写一个处理ffmpeg的实用程序。一旦我需要将图像平面从一个指针复制到另一个指针。从AVPicture结构到我自己的。 以下是一些来源 我自己的框架结构。在构造函数中分配的内存,在析构函数中解除分配的内存 template <class DataType> struct Frame { DataType* data; //!< Pointer to image data int f_type; //!< Type of color space
template <class DataType>
struct Frame
{
DataType* data; //!< Pointer to image data
int f_type; //!< Type of color space (ex. RGB, HSV, YUV)
int timestamp; //!< Like ID of frame. Time of this frame in the video file
int height; //!< Height of frame
int width; //!< Width of frame
Frame(int _height, int _width, int _f_type=0):
height(_height),width(_width),f_type(_f_type)
{
data = new DataType[_width*_height*3];
}
~Frame()
{
delete[] data;
}
};
模板
结构框架
{
数据类型*数据;//!<指向图像数据的指针
int f_type;//!<颜色空间的类型(例如RGB、HSV、YUV)
int timestamp;//!<类似于帧的ID。视频文件中此帧的时间
内部高度;/!<框架高度
int width;/!<帧的宽度
帧(整数高度,整数宽度,整数类型=0):
高度(_高度)、宽度(_宽度)、f类型(_f类型)
{
数据=新数据类型[_宽度*_高度*3];
}
~Frame()
{
删除[]数据;
}
};
下面是执行转换的主循环。如果注释了带有memcpy的行,则根本没有内存泄漏。但是如果我取消注释它,内存泄漏就会出现
for(int i = begin; i < end; i++)
{
AVPicture pict;
avpicture_alloc(&pict, PIX_FMT_BGR24, _width, _height);
std::shared_ptr<Frame<char>> frame(new Frame<char>(_height, _width, (int)PIX_FMT_BGR24));
sws_scale(ctx, frame_list[i]->data, frame_list[i]->linesize, 0, frame_list[i]->height, pict.data, pict.linesize);
memcpy(frame->data,pict.data[0],_width*_height*3);
//temp_to_add->push_back(std::shared_ptr<Frame<char>>(frame));
avpicture_free(&pict);
}
for(int i=begin;i数据,帧_列表[i]->线条尺寸,0,帧_列表[i]->高度,图像数据,图像线条尺寸);
memcpy(帧->数据,图像数据[0],_宽度*高度*3);
//temp_to_add->push_back(std::shared_ptr(frame));
avpicture_免费(&pict);
}
我一直在尝试很多事情,比如:通过malloc分配内存,通过free释放内存,手动将内存从pict复制到frame(for循环),使用std::copy和avpicture_布局,这是ffmpeg的辅助功能。没什么帮助。
所以问题是:我是否忘记了一些重要的事情
我将感谢你的每一个回答 您的类很容易在复制构造或赋值时泄漏内存。您应该提供一个显式的复制构造函数和赋值运算符(至少禁止作为私有):
private:
框架(const框架和rhs);
帧和运算符=(常量帧和rhs);
您确定这是泄漏,而不仅仅是内存出现故障吗
如果调用malloc()
或new
,该语言将允许您访问内存。然而,操作系统(对于虚拟内存操作系统,如Windows、Linux或MacOS X),在您尝试使用该内存之前,实际上不会将页面作为任务工作集的一部分
如果您认为是由于memcpy()
导致Windows的process explorer中的进程大小增加而导致泄漏,那么这是一个错误的结论。即使您free()
或删除对象,进程大小也不一定会减小
以这两个测试用例为例。很明显,两者都没有泄漏
// test 1
int main()
{
volatile int *data = (volatile int *)malloc( (1 << 24) * sizeof(int) );
free((void *)data);
// Spin until killed, so we're visible in the process explorer as long as needed
while (1)
;
}
//测试1
int main()
{
volatile int*data=(volatile int*)malloc((1)您的类在复制构造和分配时如何处理分配的内存?您没有遵循3的规则!您为什么认为您正在泄漏内存?您使用什么来检测泄漏?Valgrind?我使用process explorer(Windows)还有数学。这不是最好的方法,我希望你能给我一些关于如何查找漏洞的建议。我试过Intel Inspector,但它对我没有帮助。我正在泄漏内存,因为如果memcpy没有注释,内存使用量会线性增加。嗯,还有一点:我想像素数据可能是对齐的表示,它们不太可能被压缩o正好是24位。如果代码是您发布的,则shared_ptr
的内容将在每次迭代结束时被销毁,因此不可能发生泄漏。memcpy
函数不执行任何内存管理。好吧,谢谢,获取它,但没有任何更改。没有分配和调用复制构造或者在下面的代码中。这种情况下是其他原因导致了泄漏。但我不明白到底是什么原因。换句话来说@DanielKO的评论:“为什么你认为你在泄漏内存?”你是否尝试自己运行此代码?我试过了,你猜似乎是错的。我尝试使用new和malloc进行此操作。这两种方法的结果相同:在释放内存之前,pRogram一直在使用64MB,但一旦我们称之为delete或free,它就使用了大约500kB。怎么了?我至少在Linux上这样做过,当我malloc()
内存并使用它时,与malloc()
内存并没有使用它时,我看到了很大的不同。“虚拟内存”大小经常增加,但不是“驻留集大小”@Zoellick:好的,我在Windows XP SP3上运行了一个测试,使用了MinGW GCC。在malloc()
之后,我的进程大小没有增加。在第一次写入malloc
区域时,我的进程大小跳跃了Imalloc
(用64K,1MB,64MB测试)当我free()
在该区域,我的进程大小再次下降。如果我malloc()
和free()
没有中间写入,我的进程大小不会改变。我在任务管理器的“进程”选项卡下查看了进程大小。
// test 1
int main()
{
volatile int *data = (volatile int *)malloc( (1 << 24) * sizeof(int) );
free((void *)data);
// Spin until killed, so we're visible in the process explorer as long as needed
while (1)
;
}
// test 2
int main()
{
volatile int *data = (volatile int *)malloc( (1 << 24) * sizeof(int) );
int i;
for (i = 0; i < (1 << 24); i++)
data[i] = i;
free((void *)data);
// Spin until killed, so we're visible in the process explorer as long as needed
while (1)
;
}