Mat::release leaks memory (Bug #3001)
Description
The implementation of Mat::release will set the data pointer (and other members) to 0 even if refcount is > 0. Braces should be added to the if clause so the 3 last lines are executed only if deallocate takes place.
inline void Mat::release() { if( refcount && CV_XADD(refcount, -1) == 1 ) deallocate(); data = datastart = dataend = datalimit = 0; size.p[0] = 0; refcount = 0; }
The attached program demonstrates the problem. Even though the reference count has been incremented (with addref) and is equal to 2 before the call to release, release() will set the data pointer to 0 (and the data isn't deallocated because deallocate() isn't called).
~|⇒ ./cvmat_release_bug m.refcount : 2 m.data : 0
Associated revisions
Merge pull request #3001 from asmorkalov:cuda_6.5_support
History
Updated by Vadim Pisarevsky almost 12 years ago
the behavior is correct. Several cv::Mat headers may reference the same data but nevertheless, when you call Mat::release(), you explicitly tell that certain Mat header does not reference the data anymore and hence the pointer to data in this very header (not the other headers referencing the same data) is set to 0.
- Status changed from Open to Cancelled