Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 898279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:01:06+00:00 2026-05-15T15:01:06+00:00

I have a small wrapper which centralize what’s relative to threads : class Thread

  • 0

I have a small wrapper which centralize what’s relative to threads :

class Thread {
protected:
    boost::thread *m_thread;

    virtual void work() = 0;

    void do_work() {
        work();
    }

public:
    Thread() : m_thread(NULL) {}
    virtual ~Thread() {
        catch_up();
        delete m_thread;
    }

    inline void catch_up() {
        if(m_thread != NULL) {
            m_thread->join();
        }
    }

    void run() {
        m_thread = new boost::thread(boost::bind(&Thread::do_work, boost::ref(*this)));
    }
};

When I implement it, say with the following :

class A : public Thread {
    void work() {}
};

At :

A a; a.run();

I got a runtime termination with a pretty “pure virtual method called” displayed. I think it’s the boost::bind argument, but I don’t know how to say “Use virtual pure implementation”…

Thanks aforehand.

Regards,

Mister Mystère

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:01:07+00:00Added an answer on May 15, 2026 at 3:01 pm

    Your crash happens only when your program exits immediately: it calls class A’s destructor which finishes and calls Thread’s destructor before the newly started thread had a chance to be scheduled. The thread then calls your virtual function, but class A no longer exists, so it attemps to call Thread’s do_work(), which calls the pure virtual work(). Here’s your program with extra outputs:

    run() started 
    run() ended
    ~A() started
    ~A() ended
    ~Thread() started
    catch_up() started
    do_work() started
    pure virtual method called
    

    Standard-wise, I think this is undefined behavior because the object’s lifetime has already ended (destructor call began) when a reference to it (boost::ref(*this)) was used to call do_work() from the thread.

    Solution: let your thread execute before you destruct your object:

    A a; a.run();
    a.catch_up();
    

    Or, as boost.thread documentation says, “the user of Boost.Thread must ensure that the referred-to object outlives the newly-created thread of execution.”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a small wrapper class that uses ResourceManager to load string resources from
I have written a small wrapper for the dict built-in class that loads entries
I wrote a small Server class which basically is a TcpListener wrapper and ThreadPool
I have a small command-line application written in C that acts as a wrapper/launcher
I have small script in bash, which is generating graphs via gnuplot. Everything works
I am new user of Sonar. I have a small project in java which
I have the following code var input = $('#el_id'); wrapper = $(document.createElement('div')); wrapper.addClass('small'); wrapper.css('width',200);
for a small science project I set up a Simulation class which holds all
I have a small gradle build which is using a 3rd party plugin it
I have a small C++ application which I imported Objective-C classes. It works as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.