@@ -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
4039PopSift::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
5957PopSift::~PopSift ()
6058{
59+ if (_isInit)
60+ {
61+ uninit ();
62+ }
6163}
6264
6365bool 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
122124void 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
139136SiftJob* PopSift::enqueue ( int w,
@@ -171,20 +168,20 @@ SiftJob* PopSift::enqueue( int w,
171168void 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
182179void 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( )
238235SiftJob::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 )
255252SiftJob::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+ }
0 commit comments