Skip to content

Commit 3e624d2

Browse files
authored
Merge pull request #71 from alicevision/dev/autoUninit
Improved resource releasing
2 parents 1416604 + da4aeda commit 3e624d2

3 files changed

Lines changed: 75 additions & 39 deletions

File tree

src/application/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cmake_minimum_required(VERSION 3.0)
2-
project(PopsiftDemo)
2+
project(PopsiftDemo LANGUAGES CXX)
33

44
if(TARGET popsift)
55
# when compiled in the repository the target is already defined

src/popsift/popsift.cpp

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@ PopSift::PopSift( const popsift::Config& config, popsift::Config::ProcessingMode
2626
_pipe._unused.push( new popsift::ImageFloat );
2727
_pipe._unused.push( new popsift::ImageFloat );
2828
}
29-
_pipe._pyramid = 0;
3029

3130
configure( config, true );
3231

33-
_pipe._thread_stage1 = new boost::thread( &PopSift::uploadImages, this );
32+
_pipe._thread_stage1.reset( new boost::thread( &PopSift::uploadImages, this ));
3433
if( mode == popsift::Config::ExtractingMode )
35-
_pipe._thread_stage2 = new boost::thread( &PopSift::extractDownloadLoop, this );
34+
_pipe._thread_stage2.reset( new boost::thread( &PopSift::extractDownloadLoop, this ));
3635
else
37-
_pipe._thread_stage2 = new boost::thread( &PopSift::matchPrepareLoop, this );
36+
_pipe._thread_stage2.reset( new boost::thread( &PopSift::matchPrepareLoop, this ));
3837
}
3938

4039
PopSift::PopSift( ImageMode imode )
@@ -50,19 +49,22 @@ PopSift::PopSift( ImageMode imode )
5049
_pipe._unused.push( new popsift::ImageFloat );
5150
_pipe._unused.push( new popsift::ImageFloat );
5251
}
53-
_pipe._pyramid = 0;
5452

55-
_pipe._thread_stage1 = new boost::thread( &PopSift::uploadImages, this );
56-
_pipe._thread_stage2 = new boost::thread( &PopSift::extractDownloadLoop, this );
53+
_pipe._thread_stage1.reset( new boost::thread( &PopSift::uploadImages, this ));
54+
_pipe._thread_stage2.reset( new boost::thread( &PopSift::extractDownloadLoop, this ));
5755
}
5856

5957
PopSift::~PopSift()
6058
{
59+
if(_isInit)
60+
{
61+
uninit();
62+
}
6163
}
6264

6365
bool PopSift::configure( const popsift::Config& config, bool force )
6466
{
65-
if( _pipe._pyramid != 0 ) {
67+
if( _pipe._pyramid != nullptr ) {
6668
return false;
6769
}
6870

@@ -97,7 +99,7 @@ bool PopSift::private_init( int w, int h )
9799
float upscaleFactor = _config.getUpscaleFactor();
98100
float scaleFactor = 1.0f / powf( 2.0f, -upscaleFactor );
99101

100-
if( p._pyramid != 0 ) {
102+
if( p._pyramid != nullptr ) {
101103
p._pyramid->resetDimensions( _config,
102104
ceilf( w * scaleFactor ),
103105
ceilf( h * scaleFactor ) );
@@ -121,19 +123,14 @@ bool PopSift::private_init( int w, int h )
121123

122124
void PopSift::uninit( )
123125
{
124-
_pipe._queue_stage1.push( 0 );
125-
_pipe._thread_stage2->join();
126-
_pipe._thread_stage1->join();
127-
delete _pipe._thread_stage2;
128-
delete _pipe._thread_stage1;
129-
130-
while( !_pipe._unused.empty() ) {
131-
popsift::ImageBase* img = _pipe._unused.pull();
132-
delete img;
126+
if(!_isInit)
127+
{
128+
std::cout << "[warning] Attempt to release resources from an uninitialized instance" << std::endl;
129+
return;
133130
}
131+
_pipe.uninit();
134132

135-
delete _pipe._pyramid;
136-
_pipe._pyramid = 0;
133+
_isInit = false;
137134
}
138135

139136
SiftJob* PopSift::enqueue( int w,
@@ -171,20 +168,20 @@ SiftJob* PopSift::enqueue( int w,
171168
void PopSift::uploadImages( )
172169
{
173170
SiftJob* job;
174-
while( ( job = _pipe._queue_stage1.pull() ) != 0 ) {
171+
while( ( job = _pipe._queue_stage1.pull() ) != nullptr ) {
175172
popsift::ImageBase* img = _pipe._unused.pull();
176173
job->setImg( img );
177174
_pipe._queue_stage2.push( job );
178175
}
179-
_pipe._queue_stage2.push( 0 );
176+
_pipe._queue_stage2.push( nullptr );
180177
}
181178

182179
void PopSift::extractDownloadLoop( )
183180
{
184181
Pipe& p = _pipe;
185182

186183
SiftJob* job;
187-
while( ( job = p._queue_stage2.pull() ) != 0 ) {
184+
while( ( job = p._queue_stage2.pull() ) != nullptr ) {
188185
popsift::ImageBase* img = job->getImg();
189186

190187
private_init( img->getWidth(), img->getHeight() );
@@ -217,7 +214,7 @@ void PopSift::matchPrepareLoop( )
217214
Pipe& p = _pipe;
218215

219216
SiftJob* job;
220-
while( ( job = p._queue_stage2.pull() ) != 0 ) {
217+
while( ( job = p._queue_stage2.pull() ) != nullptr ) {
221218
popsift::ImageBase* img = job->getImg();
222219

223220
private_init( img->getWidth(), img->getHeight() );
@@ -238,12 +235,12 @@ void PopSift::matchPrepareLoop( )
238235
SiftJob::SiftJob( int w, int h, const unsigned char* imageData )
239236
: _w(w)
240237
, _h(h)
241-
, _img(0)
238+
, _img(nullptr)
242239
{
243240
_f = _p.get_future();
244241

245242
_imageData = (unsigned char*)malloc( w*h );
246-
if( _imageData != 0 ) {
243+
if( _imageData != nullptr ) {
247244
memcpy( _imageData, imageData, w*h );
248245
} else {
249246
cerr << __FILE__ << ":" << __LINE__ << " Memory limitation" << endl
@@ -255,12 +252,12 @@ SiftJob::SiftJob( int w, int h, const unsigned char* imageData )
255252
SiftJob::SiftJob( int w, int h, const float* imageData )
256253
: _w(w)
257254
, _h(h)
258-
, _img(0)
255+
, _img(nullptr)
259256
{
260257
_f = _p.get_future();
261258

262259
_imageData = (unsigned char*)malloc( w*h*sizeof(float) );
263-
if( _imageData != 0 ) {
260+
if( _imageData != nullptr ) {
264261
memcpy( _imageData, imageData, w*h*sizeof(float) );
265262
} else {
266263
cerr << __FILE__ << ":" << __LINE__ << " Memory limitation" << endl
@@ -317,3 +314,27 @@ popsift::FeaturesDev* SiftJob::getDev()
317314
return dynamic_cast<popsift::FeaturesDev*>( _f.get() );
318315
}
319316

317+
void PopSift::Pipe::uninit()
318+
{
319+
_queue_stage1.push( nullptr );
320+
if(_thread_stage2 != nullptr)
321+
{
322+
_thread_stage2->join();
323+
_thread_stage2.reset(nullptr);
324+
}
325+
if(_thread_stage1 != nullptr)
326+
{
327+
_thread_stage1->join();
328+
_thread_stage1.reset(nullptr);
329+
}
330+
331+
while( !_unused.empty() )
332+
{
333+
popsift::ImageBase* img = _unused.pull();
334+
delete img;
335+
}
336+
337+
delete _pyramid;
338+
_pyramid = nullptr;
339+
340+
}

src/popsift/popsift.h

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,18 @@ class PopSift
7474
{
7575
struct Pipe
7676
{
77-
boost::thread* _thread_stage1;
78-
boost::thread* _thread_stage2;
77+
std::unique_ptr<boost::thread> _thread_stage1;
78+
std::unique_ptr<boost::thread> _thread_stage2;
7979
boost::sync_queue<SiftJob*> _queue_stage1;
8080
boost::sync_queue<SiftJob*> _queue_stage2;
8181
boost::sync_queue<popsift::ImageBase*> _unused;
82-
popsift::ImageBase* _current;
8382

84-
popsift::Pyramid* _pyramid;
83+
popsift::Pyramid* _pyramid{nullptr};
84+
85+
/**
86+
* @brief Release the allocated resources, if any.
87+
*/
88+
void uninit();
8589
};
8690

8791
public:
@@ -92,11 +96,15 @@ class PopSift
9296
};
9397

9498
public:
99+
100+
PopSift() = delete;
101+
PopSift(const PopSift&) = delete;
102+
95103
/* We support more than 1 streams, but we support only one sigma and one
96104
* level parameters.
97105
*/
98-
PopSift( ImageMode imode = ByteImages );
99-
PopSift( const popsift::Config& config,
106+
explicit PopSift( ImageMode imode = ByteImages );
107+
explicit PopSift( const popsift::Config& config,
100108
popsift::Config::ProcessingMode mode = popsift::Config::ExtractingMode,
101109
ImageMode imode = ByteImages );
102110
~PopSift();
@@ -118,10 +126,14 @@ class PopSift
118126
int h,
119127
const float* imageData );
120128

121-
/** deprecated */
129+
/**
130+
* @deprecated
131+
* */
122132
inline void uninit( int /*pipe*/ ) { uninit(); }
123133

124-
/** deprecated */
134+
/**
135+
* @deprecated
136+
**/
125137
inline bool init( int /*pipe*/, int w, int h ) {
126138
_last_init_w = w;
127139
_last_init_h = h;
@@ -161,8 +173,11 @@ class PopSift
161173
*/
162174
popsift::Config _shadow_config;
163175

164-
int _last_init_w; /* to support depreacted interface */
165-
int _last_init_h; /* to support depreacted interface */
176+
int _last_init_w{}; /* to support deprecated interface */
177+
int _last_init_h{}; /* to support deprecated interface */
166178
ImageMode _image_mode;
179+
180+
/// whether the object is initialized
181+
bool _isInit{true};
167182
};
168183

0 commit comments

Comments
 (0)