OpenCV memory leaking management in C/C++ (OpenCV 内存泄露)

本文介绍了使用OpenCV进行图像处理时如何有效地管理内存,避免内存泄漏。文中列举了创建和释放资源的方法,例如cvCreateImage与cvReleaseImage等,并提供了常见内存泄漏场景的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Original page: http://www.andol.info/hci/963.htm

If you’re new to OpenCV, you need to know exactly how to manage all the huge amounts of memory you’re using. C/C++ isn’t a garbage collected language (like Java), so you need to manually release memory as soon as its use is over. If you don’t, your program could use up hundreds of MBs of highly valuable RAM… and often even crash (out-of-memory errors?)

It can be a daunting task to hunt exactly where memory needs to be released. So I’ve compiled this short list of places where you should look out for memory leaks.

Create it, then Release it

If you create something, make sure you release it before “returning”. This is probably the very first thing you should check when fixing memory leak problems with OpenCV. For example, if you do a cvCreateImage, make sure you do a cvReleaseImage. There are many things you can create. Here are some functions that “create” and their corresponding “release” functions

cvCreateImagecvReleaseImage
cvCreateImageHeadercvReleaseImageHeader
cvCreateMatcvReleaseMat
cvCreateMatNDcvReleaseMatND
cvCreateDatacvReleaseData
cvCreateSparseMatcvReleaseSparseMat
cvCreateMemStoragecvReleaseMemStorage
cvCreateGraphScannercvReleaseGraphScanner
cvOpenFileStoragecvReleaseFileStorage
cvAlloccvFree



One warning though: If you create something and want to return it, don’t release it. Lets say a function that creates a checkerboard image and returns it. If you release the image before returning it, you’re freeing all memory that stores the image data. And when you try accessing memory that isn’t yours, you get a crash.

Release returned structures

This is the second thing you should check for. Often, once you return a structure (say, an image).. you forget about it.

Multiple Memory Allocations

This is the third thing you should check for: Allocating memory, and then changing the pointer itself. Here’s some example code:

This function creates a memory leak. First, you allocate some memory for image . Then, you call the function CreateCheckerBoard. This function itself creates new memory. And image   now points to this new memory. The memory created in the first step is lost forever. No variable points to it. A memory leak. To fix this, you need to modify the code like this:

If you return a sequence, release its storage

There are many instances where you use the CvSeq data structure. And often you might want to return this structure for further use. If you release its storage (a CvMemStorage structure) within the function itself, you’d free the memory where the sequence is stored. And then you’d try and access it in the calling function. Again, crash.

A temporary fix would be to just erasing the cvReleaseMemStorage statement… but that would mean lots of memory.
To fix this, you don’t release the memory in the function itself. You release it in the calling function like this:

storage   is a member of the CvMemStorage structure that always points to the memory where its stored.

Again, this is just an example. There are more structures where a similar situation could arise.

Dependence on other structures

I quite recently discovered this memory leak. To explain this, I’ll use an example: Lets say you find out the contours of an image. OpenCV would return a “linked list” type structure calledCvSeq . You decide to access the third element of this linked list. OpenCV returns a pointer to the third element. All going great till this moment.

Now you decide to save all the points of this contour (the third element) in a data structure of your own. Since this is an array of points, you do something like:

You set the pointer to equal to the thirdcontour . This is the bug. If you release the storage of the sequence (which you should), mystructure   has a bad pointer. To fix this, allocate new memory to mystructure->points  and then copy contents of thirdcontour->points … something like this:

This creates new memory for your structure and then copies each element there. Once you’ve done this, you can release the storage of the sequence without fear.

 

[Original source from: LiquidMetal , all rights reserved by original authors]

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值