Hi, I am new to boost threads I have compiled both the 1.33 and 1.34 boost libraries and am getting the same error on both.

 

Every time I run the program I get the “boost::bad_weak_ptr” error being caught in the Start function of the Boost Thread Class.

 

Can anyone tell me what I am doing wrong?

 

Stephen

 

//------------------------------ MAIN FILE ------------------------------------------

// Standard Library

#include <iostream>

#include <string>

using namespace std;

// Boost Library Includes

#include <boost/thread.hpp>

// Customer Class Includes

#include "BoostThread.h"

 

int main()

{

      cout << "Testing Boost Thread....\n";

     

      // Create Boost Class

      BoostThread boostThread("test");

 

      // Start the Class in it's own thread

      boostThread.Start();

 

      // Initialise the Time Variables

  boost::xtime xt;

  boost::xtime_get(&xt, boost::TIME_UTC);                  

  xt.sec += 5;

  // Sleep

  boost::thread::sleep(xt);

 

  // Stop the thread

  boostThread.Stop();

 

 

}

 

 

//------------------------------ HEADER CODE ------------------------------------------

#ifndef __BOOSTTHREAD_H__

#define __BOOSTTHREAD_H__

 

// Standard Library

#include <iostream>

#include <string>

using namespace std;

// Boost Libraries

#include <boost/enable_shared_from_this.hpp>

#include <boost/shared_ptr.hpp>

#include <boost/thread.hpp>

#include <boost/weak_ptr.hpp>

// Pre Declarations

class BoostThread;

typedef boost::shared_ptr<BoostThread> BoostThreadPtr;

typedef boost::weak_ptr<BoostThread> BoostThreadWeakPtr;

 

class BoostThread : public boost::enable_shared_from_this<BoostThread>

{

public:

      ///

  /// Constructor

  ///

      explicit BoostThread(std::string);

 

      ///

  /// Destructor

  ///

      ~BoostThread();

 

      ///

  /// Starts the class on it's own thread.

  ///

  void Start();

 

  ///

  /// Stops the thread.

  ///

  void Stop()

  { active = false; }

 

      ///

  /// Run Functionality.

  ///

  void Run();

 

      ///

  /// Inner thread runner class

  ///

  class Runner

  {

  public:

            ///

            /// Construct a Runner object for a ping server.

            ///

            /// @param server The server to run.

            ///

            Runner(BoostThreadPtr _threadRunner)

                              :      threadRunner(_threadRunner) { }

 

            ///

            /// Thread run function.

            ///

            void operator()() const { threadRunner->Run(); }

 

  private:

    BoostThreadPtr threadRunner;      ///< The ping server to be started.

  };

 

protected:

 

private:

      std::string           threadname;        ///< the name of the thread

      bool                  active;            ///< active flag, set to false to exit thread

};

#endif // __BOOSTTHREAD_H__

 

//---------------------------- SOURCE CODE --------------------------------------------

 

#include "BoostThread.h"

BoostThread::BoostThread(std::string _threadName)

: threadname(_threadName)

, active(true)

{

      cout << "BoostThread Constructor, thread name = [" << threadname << "]\n";

}

/////////////////////////////////////////////////////////////////////////////////

//

BoostThread::~BoostThread()

{

}

/////////////////////////////////////////////////////////////////////////////////

//

void BoostThread::Start()

{

      try

  {

    std::cout << "starting class in thread\n";

    const boost::function0<void> newConnFn = BoostThread::Runner(shared_from_this());

    std::cout << "function pointer = [" << newConnFn << "]\n";

    boost::thread runThread(newConnFn);

  }

  catch(std::exception &e)

  {

    std::cout << "Start Error = [" << e.what() << "]\n";

      }

 

}

/////////////////////////////////////////////////////////////////////////////////

//

void BoostThread::Run()

{

      std::cout << "Running " << threadname << std::endl;

}