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.