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

  • SEARCH
  • Home
  • 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 8466203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:16:20+00:00 2026-06-10T15:16:20+00:00

Boost::function is throwing me exceptions one out of ten times when I try to

  • 0

Boost::function is throwing me exceptions one out of ten times when I try to assign one function1 to another function1.

Task is a typedef for boost::function1<void, void*>.

Here is the specific code:

    // the Task object sent in as Task task
    void Sleeper(void* arg)
    {
        int32_t sleepTime = *(int32_t*)arg;

        SleepCurrentThread((int32_t)sleepTime);
    }

    struct ThreadInfo
    {
        ThreadInfo() : mState(DETACHED), mTask(NULL), mArg(NULL)
        { }

        ThreadState mState;
        Task mTask;
        void* mArg;
    };

    Thread::Thread(Task task, void* arg, IMemoryAllocator& allocator, ILogManager& logger) : mAllocator(allocator), mLogger(logger)
    {
        mThreadInfo = (ThreadInfo*) mAllocator.Allocate(sizeof(ThreadInfo));  // simnple heap allocation

        mThreadInfo->mArg = arg;
        mThreadInfo->mState = Thread::RUNNING;
        mThreadInfo->mTask = task;     //<--------- throws... sometimes


        mHandle = _CreateThread(&Run, (void*)mThreadInfo);
        if (!mHandle)
            Detach();


    }

I specificly tracked it down in the boost function_template.hpp to the assignment operator, on this code, where it ultimately throws:

// Assignment from another BOOST_FUNCTION_FUNCTION
    BOOST_FUNCTION_FUNCTION& operator=(const BOOST_FUNCTION_FUNCTION& f)
    {
      if (&f == this)
        return *this;

      this->clear();
      BOOST_TRY {
        this->assign_to_own(f);        // <--- throws, and then line below re-throws
      } BOOST_CATCH (...) {
        vtable = 0;
        BOOST_RETHROW;
      }
      BOOST_CATCH_END
      return *this;
    }

Why is this? Is there anything easily spotted that is wrong with my code? Is there anything else needed?

Thanks

EDIT: I know I will be asked to use boost::threads, but I’m trying out my own wrapper around win32/pthread, (for fun)

  • 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-06-10T15:16:22+00:00Added an answer on June 10, 2026 at 3:16 pm

    Your struct has a non-trivial constructor, but you don’t call it. It leaves the Task member uninitialized. To initialize it, either allocate the whole object with new, or use placement new to initialize it as follows:

        void *mem = mAllocator.Allocate(sizeof(ThreadInfo));  // simnple heap allocation
        mThreadInfo = new(mem) ThreadInfo; // placement new
    
        mThreadInfo->mArg = arg;
        mThreadInfo->mState = Thread::RUNNING;
        mThreadInfo->mTask = task;
    

    Placement new constructs an object in an already allocated raw (uninitialized) memory.

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

Sidebar

Related Questions

I'm using boost::function for making function-references: typedef boost::function<void (SomeClass &handle)> Ref; someFunc(Ref &pointer) {/*...*/}
Consider: template < typename Something > boost::function<void()> f() { typedef typename Something::what type; return
I have a class which contains boost::function as one of its arguments. I have
//error C2784: 'HRESULT get_wrapper(T *,boost::function<R(void)>)' : //could not deduce template argument for 'boost::function<R(void)>' //from
How can I template these functions? boost::function< void(int) > binder( void(*func)(int, char), int a1,
I have a generic function: void ImageAlbum::ExpressButtonPressed( boost::function< void ( thumb::PhotoPrintThumbnail*, thumb::PhotoPrintFormat, thumb::PhotoPrintQuantity )
I have the following definition of a boost::function object: typedef boost::function<std::string (std::string, std::string)> concat;
I have this function: void func(boost::function<void(float)> cb){ //do something with cb() } It works
I'm trying to get a pointer to a function in this way: boost::function<void(float)> Function;
Can someone please explain this piece of code? struct Class { boost::function<void()> member; };

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.