Re: destroy singleton?
Andy Champ wrote:
Ron Eggler wrote:
Thank you for hints & suggestions!
Ron
In the code you've supplied, you haven't initialised pinstance. Either
we're missing something - or you are... Also, you haven't shown what
else you do after the delete, nor what steps you take to make sure it
doesn't get deleted more than once.
i do initialize pinstance like this:
[code]
GPIOcontrol * GPIOcontrol::instance(GPSData *gpsDataObj)
{
if (pinstance==NULL){//if this singleton hasn't been created yet...
pinstance=new GPIOcontrol(gpsDataObj);
}
return pinstance;
}
[/code]
and before the delete command i check if it's "!=NULL" so that should be
fine.
What's a TSPThread?
That's a class handling threads, it just initializes and destoys the thread.
it also offers flags to check if the thread is running already.
The header looks like:
[tshpthread.h]
class TSPThread
{
public:
////////////////////////////////////////////////////////////////////////////////////
/// This method check to see the thread is still active or not
///@return bool true thread is still running
/// false thraed end already
////////////////////////////////////////////////////////////////////////////////////
bool IsActive(){return (m_pThread == 0?false:true);};
///The entry point of the thread, after TSPThread class was create, call
this method to start the real thread
///This method also make sure for every TSPThread instance, there's only
one thread runs
void start();
///Constructor
TSPThread();
///Destructor
virtual ~TSPThread();
// a global flag that can be set to true by a class above to end the
thread(s)
bool EndFlag;
protected:
////////////////////////////////////////////////////////////////////////////////////
///This method suppose to tell upper module the thread is terminated
already or not.
///@return true The thread is stopped already, it's safe to remove it from
memroy.
/// false The thread is still active, please wait
////////////////////////////////////////////////////////////////////////////////////
bool GetStopFlag(){return m_bStopThread;};
private:
////////////////////////////////////////////////////////////////////////////////////
///This is the pure virutal function, all subclass should implement this
fucntion. All stuff
///You wanna do in the thread whould go here.
////////////////////////////////////////////////////////////////////////////////////
virtual void run()=0;
////////////////////////////////////////////////////////////////////////////////////
///This is a helper function for TSPThread, it makes class TSPThread know
nothing about
///subclass, but subclass keeps all features as a normal class.
///@param void *i_ptr The pointer of current subclass object
///@return static void * Dummy type for pthread_create
////////////////////////////////////////////////////////////////////////////////////
static void *StartThread(void *i_ptr);
///The flag for thread is created already or not
bool m_bThreadAlready;
///Flag forcurrent thread is stopped or not
bool m_bStopThread;
///The handler for current thread
pthread_t m_pThread;
///Mutex for thread creation critical section
pthread_mutex_t mutex_single;
};
[/tshpthread.h]
--
weeks of software engineering safe hours of planning ;)