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
public:
/** constructor */
BucketGroup();
BucketGroup(bool needAllocateGroup = true);
/** destructor */
~BucketGroup();
......@@ -1126,7 +1126,7 @@ private:
static const int MAX_B;
/** Bins (each bin is an Array object for duplicates of the same key) */
BucketGroup *table;
std::vector<BucketGroup> table;
public:
......@@ -1172,12 +1172,15 @@ length = 0;
/** constructor setting sequence's length */
bitarray( UINT64 _bits )
{
arr = NULL;
init( _bits );
}
/** initializer of private fields */
void init( UINT64 _bits )
{
if( arr )
delete[] arr;
length = (UINT32) ceil( _bits / 32.00 );
arr = new UINT32[length];
erase();
......@@ -1248,13 +1251,13 @@ UINT64 N;
cv::Mat codes;
/** Counter for eliminating duplicate results (it is not thread safe) */
bitarray *counter;
Ptr<bitarray> counter;
/** 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) */
UINT32 *xornum;
std::vector<UINT32> xornum;
/** Used within generation of binary codes at a certain Hamming distance */
int power[100];
......@@ -1293,7 +1296,7 @@ Mat descriptorsMat;
std::map<int, int> indexesMap;
/** internal MiHaser representing dataset */
Mihasher* dataset;
Ptr<Mihasher> dataset;
/** index from which next added descriptors' bunch must begin */
int nextAddedIndex;
......
......@@ -54,7 +54,7 @@ namespace line_descriptor
/* constructor */
BinaryDescriptorMatcher::BinaryDescriptorMatcher()
{
dataset = new Mihasher( 256, 32 );
dataset = Ptr<Mihasher>(new Mihasher( 256, 32 ));
nextAddedIndex = 0;
numImages = 0;
descrInDS = 0;
......@@ -83,7 +83,7 @@ void BinaryDescriptorMatcher::add( const std::vector<Mat>& descriptors )
void BinaryDescriptorMatcher::train()
{
if( !dataset )
dataset = new Mihasher( 256, 32 );
dataset = Ptr<Mihasher>(new Mihasher( 256, 32 ));
if( descriptorsMat.rows > 0 )
dataset->populate( descriptorsMat, descriptorsMat.rows, descriptorsMat.cols );
......@@ -97,7 +97,7 @@ void BinaryDescriptorMatcher::clear()
{
descriptorsMat.release();
indexesMap.clear();
dataset = 0;
dataset.release();
nextAddedIndex = 0;
numImages = 0;
descrInDS = 0;
......@@ -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 )
{
/* create and initialize a bitarray */
counter = new bitarray;
counter = makePtr<bitarray>();
counter->init( N );
UINT32 *res = new UINT32[K * ( D + 1 )];
......@@ -627,8 +627,6 @@ void BinaryDescriptorMatcher::Mihasher::batchquery( UINT32 * results, UINT32 *nu
delete[] res;
delete[] chunks;
delete counter;
}
/* execute a single query */
......@@ -769,12 +767,12 @@ BinaryDescriptorMatcher::Mihasher::Mihasher( int B_val, int _m )
(m-mplus) is the number of chunks with (b-1) bits */
mplus = B - m * ( b - 1 );
xornum = new UINT32[d + 2];
xornum.resize(d + 2);
xornum[0] = 0;
for ( int i = 0; i <= d; i++ )
xornum[i + 1] = xornum[i] + (UINT32) choose( b, i );
H = new SparseHashtable[m];
H.resize(m);
/* H[i].init might fail */
for ( int i = 0; i < mplus; i++ )
......@@ -792,8 +790,6 @@ void BinaryDescriptorMatcher::Mihasher::setK( int K_val )
/* desctructor */
BinaryDescriptorMatcher::Mihasher::~Mihasher()
{
delete[] xornum;
delete[] H;
}
/* populate tables */
......@@ -821,7 +817,6 @@ void BinaryDescriptorMatcher::Mihasher::populate( cv::Mat & _codes, UINT32 N_val
/* constructor */
BinaryDescriptorMatcher::SparseHashtable::SparseHashtable()
{
table = NULL;
size = 0;
b = 0;
}
......@@ -835,7 +830,7 @@ int BinaryDescriptorMatcher::SparseHashtable::init( int _b )
return 1;
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;
......@@ -844,7 +839,6 @@ int BinaryDescriptorMatcher::SparseHashtable::init( int _b )
/* destructor */
BinaryDescriptorMatcher::SparseHashtable::~SparseHashtable()
{
free (table);
}
/* insert data */
......@@ -860,10 +854,13 @@ UINT32* BinaryDescriptorMatcher::SparseHashtable::query( UINT64 index, int *Size
}
/* constructor */
BinaryDescriptorMatcher::BucketGroup::BucketGroup()
BinaryDescriptorMatcher::BucketGroup::BucketGroup(bool needAllocateGroup)
{
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 */
......
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