Opencv Memory Management

Source: Internet
Author: User

Now we start to make full use of opencv in our work ~~ It is found that the memory has to be manually released, and now the functions that need to be manually released are sorted out here.

Cvloadimage

Cvcloneimage

Cvcreatemat

The cvalloc function is called to allocate memory. Therefore, you must manually release the memory.

Next we will introduce how to implement automatic memory management in opencv. Speaking of automatic memory management, we will think of boost shared_ptr. Here we will introduce how to use shaprd_ptr to manage opencv memory.

The second parameter of the shared_ptr constructor can specify a specific release function. For example, constructing an object in a similar way

 
Shared_ptr <iplimage> IMG = shared_ptr <iplimage> (cvloadimage (filename), fun );

When the final IMG object is invalid, the fun (IMG) function is automatically called.

The image IMG created with cvloadimage must be released with cvreleaseimage (& IMG) or cvrelease (& IMG. That is, the pointer address must be passed in, instead of the pointer itself. The above fun () function can be used to pass in the pointer itself, so the following form cannot be used:

 
Shared_ptr <iplimage> IMG = shared_ptr <iplimage> (cvloadimage (filename), cvreleaseimage );

However, you must write a function to convert the above differences. The specific fun is as follows:

InlineVoidFun (iplimage *IMG) {cvreleaseimage (&IMG );}

In this way, you can use shared_ptr to manage the opencv memory. The preceding release functions can be represented by function objects. The details are as follows,

//You can use a function object to indicate all releases.StructReleaseobj {Public:Void Operator()(Void*Data) {cvrelease (&Data );}};Const StaticReleaseobj;

You can use releaseobj this time and pass it as the second parameter to shared_ptr.

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.