Opencv function cvcloneimage Memory leakage

Source: Internet
Author: User

Statement: articles reprinted from http://www.360doc.com/content/11/0621/16/6408986_128481830.shtml

Article 2 reposted from http://hi.baidu.com/cateyefish/item/794ab51f2535a4737a5f2526

Thank you for sharing the original article.

Article 1:

1. imgcopy = cvcloneimage (imgseries [0]); previously, the memory space must be released: cvreleaseimage (& imgcopy); imgcopy = NULL;

2. Memory leakage easily
Cvcloneimage function:
This function also causes memory leakage! Although it can be released, it is not known to be released due to the complexity of the program, because each copy is a complete copy of the image, including the header, ROI, and data. Will not overwrite the previous
Content. The compiler allocates memory space for each usage. A 752*480 size image with a leak memory of about 1 MB each time.
Solution:
Use the cvcopy function instead.
Cvcopy (psrcimg, pimg, null); // replace pimg = cvcloneimage (psrcimg );
Space must be allocated during pimg initialization; otherwise, the above functions cannot be executed.
Pimg = cvcreateimage (cvsize (imgwidht, imgheight), ipl_depth_8u, 3 );
3. Conversion from cvvimage format
Cvvimage IMG;
IMG. copyof (imgseries [0]);

Imgcopy = IMG. getimage ();

**************************************** **************************************** **************************************** **************************************** *************

Cvloadimage and cvcloneimage]

 

During the project process, some memory leaks often occur when opencv is used, and problems still occur in self-compiled programs. However, if library functions are called and used, but it annoyed me. It took a long time and practical experience to tell me how to call it. Next we will resolve some detected problems. (It may be that the level is not enough. These functions are not used properly, so you can look up your fingers)

Cvloadimage function:

We may not be aware of this function yet, but I have a deep understanding that this function is used once or twice in the program, but when processing sequential images to call this function cyclically, memory leakage may make you stunned! Even if you use cvreleaseimage (& pimg); for release at the end, the experiment shows that the release is almost impossible.

Solution:

Use the cvvimage class instead. Use the load function of the cvvimage class.

The procedure is as follows:

// Variable definition:

Cvvimage psrcimg;

Iplimag * psrcimgcopy; // use the iplimag variable to make a copy. After all, iplimag class processing is convenient.

// Obtain the image:

Psrcimg. Load (STR); // STR indicates the cstring image file name.
Psrcimgcopy = psrcimg. getimage (); // copy the image data of psrcimg.

// Release the memory

The psrcimg variable does not need to be released each time, because each load operation overwrites the previous memory area. Psrcimgcopy is the same.

However, the program should be released at the end of the process to avoid Memory leakage or others think you forgot.

Cvreleaseimage (& psrcimgcopy );
Psrcimg. Destroy ();

However, to release psrcimgcopy correctly, you must create the following statement:

Psrcimgcopy = cvcreateimage (cvsize (imgwidht, imgheight), ipl_depth_8u, 3 );

// Imgwidht, imgheight indicates the image width and height.

Cvcloneimage function:

This function also causes memory leakage! Although it can be released, it is not known to be released due to the complexity of the program, because each copy is a complete copy of the image, including the header, ROI, and data. Does not overwrite the previous content. The compiler allocates memory space for each usage. A 752*480 size image with a leak memory of about 1 MB each time.

Solution:

Use the cvcopy function instead.

Cvcopy (psrcimg, pimg, null); // replace pimg = cvcloneimage (psrcimg );

Space must be allocated during pimg initialization; otherwise, the above functions cannot be executed.

Pimg = cvcreateimage (cvsize (imgwidht, imgheight), ipl_depth_8u, 3 );

 

I have been writing Algorithm programs with opencv recently, but the functions of cvcloneimage, cvcopyimage, cvclonemat, and cvcopymat have made me suffer for a while. The program code has no problems, but I cannot get the results, in subfunctions, the returned values are not what I want at all. Because the code is huge, I have not found the problem. So I set breakpoints one by one and debug them step by step, at last, we found that the problem was caused by the misuse of functions such as cvcloneimage, cvcopyimage, cvclonemat, and cvcopymat. cvcloneimage and cvclonemat opened up a new space for Defined variables while assigning values, cvcopyimage and cvcopymat only copy values and do not allocate a space to the assignment object. Therefore, cvcloneimage and cvclonemat are only suitable for variable definition and must not be used in algorithm processing, otherwise, a new address space will be generated, and the pointer address of the assigned object will be changed, which will lead to unpredictable errors in the entire program. The most obvious mistake is that you originally wanted to send the new variable value in the subfunction back to the previous function. However, since the pointer point has changed, the returned value will not change. We recommend that you use cvcopyimage and cvcopymat for copying between programs.

Therefore, when using opencv functions, different functions implement the same function, but you must pay attention to the differences between them. Otherwise, it will make you very painful. It is really annoying to look for such errors.

Http://benson.is-programmer.com/posts/21042.html

Dramatic Stage 1: Problems

When using opencv recently, we found that there are some problems in the memory management of opencv in the image function section. When cvcloneimage () of an image using iplimage is called, the memory cannot be completely released. In the real-time video processing program, the program runs along with the program, and the system memory is easily exhausted.

For example, let's look at the following simple code:

# Include "cv. H"
# Include "highgui. H"
# Pragma comment (Lib, "cv. lib ")
# Pragma comment (Lib, "highgui. lib ")
# Pragma comment (Lib, "cxcore. lib ")

Int _ tmain (INT argc, _ tchar * argv [])
{
Cvcapture * capture = cvcreatecameracapture (0 );
Iplimage * frame;
Cvnamedwindow ("exampleshow", cv_window_autosize );
While (1)
{
Frame = cvqueryframe (capture );
If (! Frame)
Break;
Cvshowimage ("exampleshow", frame );
Char c = cvwaitkey (33 );
If (C = 27)
Break;
}
Cvreleasecapture (& capture );
Cvdestroywindow ("exampleshow ");
Return 0;
}

Run the program. Open the resource manager and you can see that the occupied "memory usage" remains stable. If you modify the above program, the change is as follows:

# Include "cv. H"
# Include "highgui. H"
# Pragma comment (Lib, "cv. lib ")
# Pragma comment (Lib, "highgui. lib ")
# Pragma comment (Lib, "cxcore. lib ")

Int _ tmain (INT argc, _ tchar * argv [])
{
Cvcapture * capture = cvcreatecameracapture (0 );
Iplimage * frame;
Iplimage * climage;
Cvnamedwindow ("exampleshow", cv_window_autosize );
Cvnamedwindow ("example_clone", cv_window_autosize );
While (1)
{
Frame = cvqueryframe (capture );
If (! Frame)
Break;
Cvshowimage ("exampleshow", frame );
Climage = cvcreateimage (cvsize (frame-> width, frame-> height), frame-> depth, frame-> nchannels );
Climage = cvcloneimage (FRAME );
Cvshowimage ("example_clone", climage );
Char c = cvwaitkey (33 );
If (C = 27)
Break;
Cvreleaseimage (& climage );
}
Cvreleasecapture (& capture );
Cvdestroywindow ("exampleshow ");
Cvdestroywindow ("example_clone ");
Return 0;
}

Similarly, when you run the program and open the resource manager, you can see that the memory usage of the program is increasing. Although the copied image is released in the program, in fact, it does not have much effect!

Although this problem has been found, we have also seen discussions on this issue on the Internet. We have tried several methods and found that it does not work. Ask the question here and continue exploring.

Dramatic Stage 2: emergence of "improvement methods"

In addition to cvcloneimage () and cvloadimage (), we can see that there is also a memory leakage problem on the network. The final effective solution is to use cvcopy () to replace cvcloneimage () in the Code. At this time, there will be no increasing memory. Cvloadimage () can be used to load images in the cvvimage class, and then copy them to the target image.

Dramatic 3: capture and analyze the real cause

Cause Analysis of memory cannot be released:

Today, I accidentally remembered that when I looked at the above code, I found that there was a sentence in it: climage = cvcreateimage (cvsize (frame-> width, frame-> height), frame-> depth, frame-> nchannels );

Apply for a space from the memory for storing the target image. The real cause of Memory leakage is this. When cvcloneimage () is used, it is actually a complete copy of the image header, data, and ROI pointed to by the source image pointer, which is placed in a new memory area, the function points the target image to a new memory, but the area originally allocated with cvcreateimage () is not correctly released and becomes a "hanging address area ". When cvreleaseimage () is called later, the region to which it points is released.

To avoid this problem, call cvreleaseimage () before cvcloneimage () to release the previously allocated address area. Then, execute the clone function cvcloneimage. You can also directly call the clone operation without allocating space. In another method, if you use the cvcopy () function, because this function does not allocate space for image pointers, you need to use cvcreateimage () to allocate a region first, call the copy function cvcopy () to assign values to the image. In this way, the address area indicated by the image pointer is finally released. Both methods will not cause memory leakage.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.