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 7168241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:44:26+00:00 2026-05-28T14:44:26+00:00

I saw a strange behavior in a piece of code I wrote under Linux,

  • 0

I saw a strange behavior in a piece of code I wrote under Linux, and I would like to share it to see if someone knows the cause of that.
I had a base class and a derived class. In the base class I defined a virtual method, and in the derived class I redefined that method, with the same signature. Then I used boost bind to start a thread. Here’s a sample code:

Class Base {
public:
    virtual void DoSomething();
    virtual void Init() = 0;
    ...
}

Class Derived : public Base {
public:
    void DoSomething();
    void Init();
    ...
}

In the Init method of the Derived class I did this:

 boost::thread *t = new boost::thread(boost::bind(&Base::DoSomething, this));

The DoSomething method of the base class did what it was meant to do, while the same method of the derived class was an empty method, left there by mistake. Now, while running the code above, most of the times the DoSomething of the Base class was executed in the thread, so the application worked fine, while sometimes it did not work. After some debug I noted the mistake above, and removing the DoSomething of the derived class solved the issue. Using Eclipse in debug mode it seems that the DoSomething method of the derived class was always called, while running the application from the console worked most of the times, but not always. Is there a reason for this behavior? I mean, why sometimes the bind function used the base class method, and sometimes the same method of the derived class?

Thanks in advance

Edit in response to @pmr

It would be hard to show a full working example, I will try to show a bit how the classes are used.

First I instantiate a Derived object, then in the init function I start the thread with the initialization code shown above. The DoSomething has a while loop that iterates on a vector, but that’s not the point I think.

void Derived::Init()
{
    ...
    boost::thread *t = new boost::thread(boost::bind(&Base::DoSomething, this));
}

void Base::DoSomething()
{
    while(true) {
        ...
    }
}

void Derived::DoSomething()
{
}

As you can see in this code the Derived DoSomething method was empty, so sometimes I did not see any processing, which instead took place in the Base DoSomething function.

  • 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-28T14:44:27+00:00Added an answer on May 28, 2026 at 2:44 pm

    I guess I’ve found out the reason of this behavior: at first I called the thread constructor inside the Base class constructor. I think this is the problem, because since the base constructor was called before the derived one, sometimes the vtable was created pointing to the empty derived function, sometimes the thread was started before the vtable was created, thus the bind function used the base method, which did what it was meant for. I guess that using debug some delays were introduced, so using the debugger the thread was always bound to the derived class method, causing a wrong behavior. Also, I tried moving the thread creation inside the init function, and in that way the derived function is always called.

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

Sidebar

Related Questions

Saw this piece of code in a Ruby on Rails book. This first one
I saw some code like the following in a JSP <c:if test=<%=request.isUserInRole(RoleEnum.USER.getCode())%>> <li>user</li> </c:if>
I saw this question asked about C# I would like an answer for PHP.
I saw something like the following in our production code: select max(col_val) from table_name
One of the areas where I would like to see the use of dynamic
Last night, I experienced a strange behavior in IE8 as I saw two noticeably
Saw the code snippet like Set<Record> instances = new HashSet<Record>(); I am wondering if
this morning I saw some strange code from one of my coworkers. In an
I'm reading a book about SQL. In that book, I saw strange query below:
I saw a code snippet yesterday in one of the responses here on StackOverflow

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.