Commit 6f793bde authored by Alexander Alekhin's avatar Alexander Alekhin

Merge pull request #861 from sovrasov:line_descr_matcher_fix_memleak

parents 75adb698 a10a827c
...@@ -1095,7 +1095,7 @@ class BucketGroup ...@@ -1095,7 +1095,7 @@ class BucketGroup
public: public:
/** constructor */ /** constructor */
BucketGroup(); BucketGroup(bool needAllocateGroup = true);
/** destructor */ /** destructor */
~BucketGroup(); ~BucketGroup();
...@@ -1126,7 +1126,7 @@ private: ...@@ -1126,7 +1126,7 @@ private:
static const int MAX_B; static const int MAX_B;
/** Bins (each bin is an Array object for duplicates of the same key) */ /** Bins (each bin is an Array object for duplicates of the same key) */
BucketGroup *table; std::vector<BucketGroup> table;
public: public:
...@@ -1172,12 +1172,15 @@ length = 0; ...@@ -1172,12 +1172,15 @@ length = 0;
/** constructor setting sequence's length */ /** constructor setting sequence's length */
bitarray( UINT64 _bits ) bitarray( UINT64 _bits )
{ {
arr = NULL;
init( _bits ); init( _bits );
} }
/** initializer of private fields */ /** initializer of private fields */
void init( UINT64 _bits ) void init( UINT64 _bits )
{ {
if( arr )
delete[] arr;
length = (UINT32) ceil( _bits / 32.00 ); length = (UINT32) ceil( _bits / 32.00 );
arr = new UINT32[length]; arr = new UINT32[length];
erase(); erase();
...@@ -1248,13 +1251,13 @@ UINT64 N; ...@@ -1248,13 +1251,13 @@ UINT64 N;
cv::Mat codes; cv::Mat codes;
/** Counter for eliminating duplicate results (it is not thread safe) */ /** Counter for eliminating duplicate results (it is not thread safe) */
bitarray *counter; Ptr<bitarray> counter;
/** Array of m hashtables */ /** Array of m hashtables */
SparseHashtable *H; std::vector<SparseHashtable> H;
/** Volume of a b-bit Hamming ball with radius s (for s = 0 to d) */ /** Volume of a b-bit Hamming ball with radius s (for s = 0 to d) */
UINT32 *xornum; std::vector<UINT32> xornum;
/** Used within generation of binary codes at a certain Hamming distance */ /** Used within generation of binary codes at a certain Hamming distance */
int power[100]; int power[100];
...@@ -1293,7 +1296,7 @@ Mat descriptorsMat; ...@@ -1293,7 +1296,7 @@ Mat descriptorsMat;
std::map<int, int> indexesMap; std::map<int, int> indexesMap;
/** internal MiHaser representing dataset */ /** internal MiHaser representing dataset */
Mihasher* dataset; Ptr<Mihasher> dataset;
/** index from which next added descriptors' bunch must begin */ /** index from which next added descriptors' bunch must begin */
int nextAddedIndex; int nextAddedIndex;
......
...@@ -54,7 +54,7 @@ namespace line_descriptor ...@@ -54,7 +54,7 @@ namespace line_descriptor
/* constructor */ /* constructor */
BinaryDescriptorMatcher::BinaryDescriptorMatcher() BinaryDescriptorMatcher::BinaryDescriptorMatcher()
{ {
dataset = new Mihasher( 256, 32 ); dataset = Ptr<Mihasher>(new Mihasher( 256, 32 ));
nextAddedIndex = 0; nextAddedIndex = 0;
numImages = 0; numImages = 0;
descrInDS = 0; descrInDS = 0;
...@@ -83,7 +83,7 @@ void BinaryDescriptorMatcher::add( const std::vector<Mat>& descriptors ) ...@@ -83,7 +83,7 @@ void BinaryDescriptorMatcher::add( const std::vector<Mat>& descriptors )
void BinaryDescriptorMatcher::train() void BinaryDescriptorMatcher::train()
{ {
if( !dataset ) if( !dataset )
dataset = new Mihasher( 256, 32 ); dataset = Ptr<Mihasher>(new Mihasher( 256, 32 ));
if( descriptorsMat.rows > 0 ) if( descriptorsMat.rows > 0 )
dataset->populate( descriptorsMat, descriptorsMat.rows, descriptorsMat.cols ); dataset->populate( descriptorsMat, descriptorsMat.rows, descriptorsMat.cols );
...@@ -97,7 +97,7 @@ void BinaryDescriptorMatcher::clear() ...@@ -97,7 +97,7 @@ void BinaryDescriptorMatcher::clear()
{ {
descriptorsMat.release(); descriptorsMat.release();
indexesMap.clear(); indexesMap.clear();
dataset = 0; dataset.release();
nextAddedIndex = 0; nextAddedIndex = 0;
numImages = 0; numImages = 0;
descrInDS = 0; descrInDS = 0;
...@@ -596,7 +596,7 @@ void BinaryDescriptorMatcher::radiusMatch( const Mat& queryDescriptors, std::vec ...@@ -596,7 +596,7 @@ void BinaryDescriptorMatcher::radiusMatch( const Mat& queryDescriptors, std::vec
void BinaryDescriptorMatcher::Mihasher::batchquery( UINT32 * results, UINT32 *numres, const cv::Mat & queries, UINT32 numq, int dim1queries ) void BinaryDescriptorMatcher::Mihasher::batchquery( UINT32 * results, UINT32 *numres, const cv::Mat & queries, UINT32 numq, int dim1queries )
{ {
/* create and initialize a bitarray */ /* create and initialize a bitarray */
counter = new bitarray; counter = makePtr<bitarray>();
counter->init( N ); counter->init( N );
UINT32 *res = new UINT32[K * ( D + 1 )]; UINT32 *res = new UINT32[K * ( D + 1 )];
...@@ -627,8 +627,6 @@ void BinaryDescriptorMatcher::Mihasher::batchquery( UINT32 * results, UINT32 *nu ...@@ -627,8 +627,6 @@ void BinaryDescriptorMatcher::Mihasher::batchquery( UINT32 * results, UINT32 *nu
delete[] res; delete[] res;
delete[] chunks; delete[] chunks;
delete counter;
} }
/* execute a single query */ /* execute a single query */
...@@ -769,12 +767,12 @@ BinaryDescriptorMatcher::Mihasher::Mihasher( int B_val, int _m ) ...@@ -769,12 +767,12 @@ BinaryDescriptorMatcher::Mihasher::Mihasher( int B_val, int _m )
(m-mplus) is the number of chunks with (b-1) bits */ (m-mplus) is the number of chunks with (b-1) bits */
mplus = B - m * ( b - 1 ); mplus = B - m * ( b - 1 );
xornum = new UINT32[d + 2]; xornum.resize(d + 2);
xornum[0] = 0; xornum[0] = 0;
for ( int i = 0; i <= d; i++ ) for ( int i = 0; i <= d; i++ )
xornum[i + 1] = xornum[i] + (UINT32) choose( b, i ); xornum[i + 1] = xornum[i] + (UINT32) choose( b, i );
H = new SparseHashtable[m]; H.resize(m);
/* H[i].init might fail */ /* H[i].init might fail */
for ( int i = 0; i < mplus; i++ ) for ( int i = 0; i < mplus; i++ )
...@@ -792,8 +790,6 @@ void BinaryDescriptorMatcher::Mihasher::setK( int K_val ) ...@@ -792,8 +790,6 @@ void BinaryDescriptorMatcher::Mihasher::setK( int K_val )
/* desctructor */ /* desctructor */
BinaryDescriptorMatcher::Mihasher::~Mihasher() BinaryDescriptorMatcher::Mihasher::~Mihasher()
{ {
delete[] xornum;
delete[] H;
} }
/* populate tables */ /* populate tables */
...@@ -821,7 +817,6 @@ void BinaryDescriptorMatcher::Mihasher::populate( cv::Mat & _codes, UINT32 N_val ...@@ -821,7 +817,6 @@ void BinaryDescriptorMatcher::Mihasher::populate( cv::Mat & _codes, UINT32 N_val
/* constructor */ /* constructor */
BinaryDescriptorMatcher::SparseHashtable::SparseHashtable() BinaryDescriptorMatcher::SparseHashtable::SparseHashtable()
{ {
table = NULL;
size = 0; size = 0;
b = 0; b = 0;
} }
...@@ -835,7 +830,7 @@ int BinaryDescriptorMatcher::SparseHashtable::init( int _b ) ...@@ -835,7 +830,7 @@ int BinaryDescriptorMatcher::SparseHashtable::init( int _b )
return 1; return 1;
size = UINT64_1 << ( b - 5 ); // size = 2 ^ b size = UINT64_1 << ( b - 5 ); // size = 2 ^ b
table = (BucketGroup*) calloc( (size_t)size, sizeof(BucketGroup) ); table = std::vector<BucketGroup>(size, BucketGroup(false));
return 0; return 0;
...@@ -844,7 +839,6 @@ int BinaryDescriptorMatcher::SparseHashtable::init( int _b ) ...@@ -844,7 +839,6 @@ int BinaryDescriptorMatcher::SparseHashtable::init( int _b )
/* destructor */ /* destructor */
BinaryDescriptorMatcher::SparseHashtable::~SparseHashtable() BinaryDescriptorMatcher::SparseHashtable::~SparseHashtable()
{ {
free (table);
} }
/* insert data */ /* insert data */
...@@ -860,10 +854,13 @@ UINT32* BinaryDescriptorMatcher::SparseHashtable::query( UINT64 index, int *Size ...@@ -860,10 +854,13 @@ UINT32* BinaryDescriptorMatcher::SparseHashtable::query( UINT64 index, int *Size
} }
/* constructor */ /* constructor */
BinaryDescriptorMatcher::BucketGroup::BucketGroup() BinaryDescriptorMatcher::BucketGroup::BucketGroup(bool needAllocateGroup)
{ {
empty = 0; empty = 0;
group = std::vector < uint32_t > ( 2, 0 ); if (needAllocateGroup)
group = std::vector < uint32_t > ( 2, 0 );
else
group = std::vector < uint32_t > ( 0, 0 );
} }
/* destructor */ /* destructor */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment