Corrected long standing bug of double deletion of trackmodes. New safer destructor

This commit is contained in:
Paolo Cignoni 2008-11-13 09:33:43 +00:00
parent 2dfa6976ac
commit 16e51c9d75
2 changed files with 14 additions and 4 deletions

View File

@ -111,6 +111,7 @@ Adding copyright.
#include <GL/glew.h> #include <GL/glew.h>
#include "trackball.h" #include "trackball.h"
#include<set>
#include <wrap/gl/math.h> #include <wrap/gl/math.h>
#include <wrap/gl/space.h> #include <wrap/gl/space.h>
@ -131,12 +132,16 @@ Trackball::Trackball(): current_button(0), current_mode(NULL), inactive_mode(NUL
Trackball::~Trackball() Trackball::~Trackball()
{ {
// Note: people ofter maps different keys to the same modes.
// so we should avoid double deletion of these double referenced modes.
std::set<TrackMode *> goodModes;
std::map<int, TrackMode *>::iterator it; std::map<int, TrackMode *>::iterator it;
for(it = modes.begin(); it != modes.end(); it++) for(it = modes.begin(); it != modes.end(); it++)
{ if ((*it).second) goodModes.insert( (*it).second);
if ((*it).second)
delete (*it).second; std::set<TrackMode *>::iterator its;
} for(its = goodModes.begin(); its != goodModes.end(); its++)
delete *its;
} }

View File

@ -199,6 +199,11 @@ public:
@warning The destructor <b>does not</b> deallocate the memory allocated by setDefaultMapping(), because the application can change the modes map. This can lead to small memory leaks, so please explicitally delete any manipulator in the modes map if you are going to repeatly allocate and deallocate Trackball instances. @warning The destructor <b>does not</b> deallocate the memory allocated by setDefaultMapping(), because the application can change the modes map. This can lead to small memory leaks, so please explicitally delete any manipulator in the modes map if you are going to repeatly allocate and deallocate Trackball instances.
*/ */
~Trackball(); ~Trackball();
private:
// TriMesh cannot be copied. Use Append (see vcg/complex/trimesh/append.h)
Trackball operator =(const Trackball & m){}
public:
/*! /*!
@brief Reset the trackball. @brief Reset the trackball.