Skip to content

Commit 602a4d6

Browse files
authored
Merge pull request #21 from jbfuehrer/master
fixed segfault due to double delete
2 parents 9a7d866 + 1029eff commit 602a4d6

File tree

3 files changed

+20
-23
lines changed

3 files changed

+20
-23
lines changed

src/fbow.cpp

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@
77

88
namespace fbow{
99

10-
11-
Vocabulary::~Vocabulary(){
12-
if (_data!=nullptr) AlignedFree( _data);
13-
}
14-
15-
1610
void Vocabulary::setParams(int aligment, int k, int desc_type, int desc_size, int nblocks, std::string desc_name) {
1711
auto ns= desc_name.size()<static_cast<size_t>(49)?desc_name.size():128;
1812
desc_name.resize(ns);
@@ -48,8 +42,8 @@ void Vocabulary::setParams(int aligment, int k, int desc_type, int desc_size, in
4842

4943
//give memory
5044
_params._total_size=_params._block_size_bytes_wp*_params._nblocks;
51-
_data=(char*)AlignedAlloc(_params._aligment,_params._total_size);
52-
memset( _data,0,_params._total_size);
45+
_data = Data_ptr((char*)AlignedAlloc(_params._aligment, _params._total_size), &AlignedFree);
46+
memset(_data.get(), 0, _params._total_size);
5347

5448
}
5549

@@ -152,8 +146,7 @@ fBow Vocabulary::transform(const cv::Mat &features)
152146

153147
void Vocabulary::clear()
154148
{
155-
if (_data!=0) AlignedFree(_data);
156-
_data=0;
149+
_data.reset();
157150
memset(&_params,0,sizeof(_params));
158151
_params._desc_name_[0]='\0';
159152
}
@@ -180,20 +173,19 @@ void Vocabulary::toStream(std::ostream &str)const{
180173
str.write((char*)&sig,sizeof(sig));
181174
//save string
182175
str.write((char*)&_params,sizeof(params));
183-
str.write(_data,_params._total_size);
176+
str.write(_data.get(), _params._total_size);
184177
}
185178

186179
void Vocabulary::fromStream(std::istream &str)
187180
{
188-
if (_data!=0) AlignedFree (_data);
189181
uint64_t sig;
190182
str.read((char*)&sig,sizeof(sig));
191183
if (sig!=55824124) throw std::runtime_error("Vocabulary::fromStream invalid signature");
192184
//read string
193185
str.read((char*)&_params,sizeof(params));
194-
_data=(char*)AlignedAlloc(_params._aligment,_params._total_size);
186+
_data = Data_ptr((char*)AlignedAlloc(_params._aligment, _params._total_size), &AlignedFree);
195187
if (_data==0) throw std::runtime_error("Vocabulary::fromStream Could not allocate data");
196-
str.read(_data,_params._total_size);
188+
str.read(_data.get(), _params._total_size);
197189
}
198190

199191
double fBow::score (const fBow &v1,const fBow &v2){
@@ -262,7 +254,7 @@ uint64_t Vocabulary::hash()const{
262254

263255
uint64_t seed = 0;
264256
for(uint64_t i=0;i<_params._total_size;i++)
265-
seed^= _data[i] + 0x9e3779b9 + (seed << 6) + (seed >> 2);
257+
seed^= _data.get()[i] + 0x9e3779b9 + (seed << 6) + (seed >> 2);
266258
return seed;
267259
}
268260
void fBow::toStream(std::ostream &str) const {

src/fbow.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ struct FBOW_API fBow2:std::map<uint32_t,std::vector<uint32_t>> {
5353
*/
5454
class FBOW_API Vocabulary
5555
{
56-
57-
5856
static inline void * AlignedAlloc(int __alignment,int size){
5957
assert(__alignment<256);
6058

@@ -72,16 +70,22 @@ class FBOW_API Vocabulary
7270
*(ptr-1)=(unsigned char)off; //save in prev, the offset to properly remove it
7371
return ptr;
7472
}
73+
7574
static inline void AlignedFree(void *ptr){
7675
unsigned char *uptr=(unsigned char *)ptr;
7776
unsigned char off= *(uptr-1);
7877
uptr-=off;
7978
std::free(uptr);
8079
}
80+
81+
using Data_ptr = std::unique_ptr<char[], decltype(&AlignedFree)>;
82+
8183
friend class VocabularyCreator;
84+
8285
public:
8386

84-
~Vocabulary();
87+
Vocabulary() = default;
88+
Vocabulary(Vocabulary&&) = default;
8589

8690
//transform the features stored as rows in the returned BagOfWords
8791
fBow transform(const cv::Mat &features);
@@ -125,7 +129,7 @@ class FBOW_API Vocabulary
125129
uint32_t _m_k=0;//number of children per node
126130
};
127131
params _params;
128-
char * _data=nullptr;//pointer to data
132+
Data_ptr _data = Data_ptr(nullptr, &AlignedFree);//pointer to data
129133

130134
//structure represeting a information about node in a block
131135
struct block_node_info{
@@ -190,9 +194,9 @@ class FBOW_API Vocabulary
190194

191195

192196
//returns a block structure pointing at block b
193-
inline Block getBlock(uint32_t b){assert( _data!=0);assert(b<_params._nblocks); return Block( _data+ b*_params._block_size_bytes_wp,_params._desc_size, _params._desc_size_bytes_wp,_params._feature_off_start, _params._child_off_start);}
197+
inline Block getBlock(uint32_t b) { assert(_data != 0); assert(b < _params._nblocks); return Block(_data.get() + b * _params._block_size_bytes_wp, _params._desc_size, _params._desc_size_bytes_wp, _params._feature_off_start, _params._child_off_start); }
194198
//given a block already create with getBlock, moves it to point to block b
195-
inline void setBlock(uint32_t b,Block &block){ block._blockstart= _data+ b*_params._block_size_bytes_wp;}
199+
inline void setBlock(uint32_t b, Block &block) { block._blockstart = _data.get() + b * _params._block_size_bytes_wp; }
196200

197201
//information about the cpu so that mmx,sse or avx extensions can be employed
198202
std::shared_ptr<cpu> cpu_info;

src/vocabulary_creator.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ void VocabularyCreator::create(fbow::Vocabulary &Voc, const std::vector<cv::Mat>
2929
if(!(_descType==CV_8UC1|| _descType==CV_32FC1))
3030
throw std::runtime_error("Descriptors must be binary CV_8UC1 or float CV_32FC1");
3131
if (_descType==CV_8UC1){
32-
// if (_descNBytes==32)dist_func=distance_hamming_32bytes;
33-
// else
32+
if (_descNBytes==32)
33+
dist_func=distance_hamming_32bytes;
34+
else
3435
dist_func=distance_hamming_generic;
3536
}
3637
else dist_func=distance_float_generic;

0 commit comments

Comments
 (0)