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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:45:16+00:00 2026-06-01T06:45:16+00:00

I use the following code: struct WorkData { std::string name; std::function<void(std::string)> Callback; WorkData(){}; WorkData(const

  • 0

I use the following code:

struct WorkData
{
    std::string name;
    std::function<void(std::string)> Callback;

    WorkData(){};
    WorkData(const WorkData& other)
    {
        name = other.name;
        Callback = std::ref(other.Callback);
    }
};

WorkData data; // this is the data to pass to queue_task() function bellow
data.Callback = std::bind(&ResultProcessor::Handler, resProc, std::placeholders::_1);

template <typename Functor>
void queue_task(Functor& fn, WorkData& workData )
{
    group.run([&fn, workData](){
          workData.Callback("resultComming"); // runtime ERROR- access violation
    });
}

queue_task function queues work to be done asynchronously on another thread (by calling group.run(lambda) from above). The problem that I’m experiencing is that I get an access violation when trying to call workData.Callback().

The reason I make a copy of workData inside group.run() is because I want to capture workData by value so that when the group.run() lambda executes it has a copy of the state when queue_task() was called. I would expect that workData.Callback() would execute on the instance of object passed on line:

data.Callback = std::bind(&ResultProcessor::Handler, resProc, std::placeholders::_1);

EDIT: resProc from above is alive (not destroyed) when the crash line is called

  • 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-01T06:45:18+00:00Added an answer on June 1, 2026 at 6:45 am

    The use of std::ref in your copy constructor means that you keep a reference to the old WorkData‘s Callback member, not a copy. You want Callback = other.Callback to make a copy to avoid an access violation (probably from accessing the old callback after it has been freed). In order to keep a reference to resProc in the std::function, you need to use std::ref(resProc) in the call to std::bind.

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

Sidebar

Related Questions

What is the correct way to use std::ref ? I tried following code in
Consider the following code. #include <iostream> #include <string> struct SimpleStruct { operator std::string ()
I have the following code: use strict; function isDefined(variable) { return (typeof (window[variable]) ===
I use the following code try to create an array of string vectors, I
Having the following code: template<typename T, typename OutStream = std::ostream> struct print { OutStream
I have the following code: // eventloop.go type Object interface { ActivateSlot(name string, parameters
consider the following code:- struct mystruct { int data; struct mystruct *next; }; void
The following code: template <typename S, typename T> struct foo { void bar(); };
Consider the following code struct foo { const int txt_len; const int num_len; char
I use the following code to set an alarm. struct itimerval timer; struct sigaction

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.