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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:00:42+00:00 2026-06-10T11:00:42+00:00

This is my first attempt at using std::future . I have three different files

  • 0

This is my first attempt at using std::future.

I have three different files that I want to parse at the same time. Three functions do that, resp. called parseSentences, parseTags and parseLinks. Each of them is launched in a separate thread using std::async by using a very simple lambda function: []() { parser->function(); }, where parser is a static variable and function is one of the three functions I have named previously.

int parser::start()
{
    int ret = SUCCESS;
    ASSERT( m_output != nullptr );
    static parser * thisParserInstance = this;

    // parsing files
    std::future<int> parseSentence = std::async(std::launch::async, []() { return thisParserInstance->parseSentences(); } );
    std::future<int> parseLinksResult = std::async(std::launch::async, []() { return thisParserInstance->parseLinks(); } );
    std::future<int> parseTagsResult = std::async(std::launch::async, []() { return thisParserInstance->parseTags(); } );

    // retrieving the results
    ret = parseSentence.get();
    const int linksResult = parseLinksResult.get();
    const int tagsResult = parseTagsResult.get();

    if (ret == SUCCESS)
        ret = linksResult == SUCCESS ? tagsResult : linksResult;

    return ret;
}

Now when I run my program in gdb, a segmentation fault occurs at the destruction of one of the std::future local variable. There are 2 threads running at that moment.
Thread #1’s call stack is here.
Thread #2’s call stack is here.

Note that the pointer to this in the first call stack is null, resulting in the segmentation fault.

If anyone has a clue, I would be thankful.

  • 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-10T11:00:44+00:00Added an answer on June 10, 2026 at 11:00 am

    One big problem is here:

    static parser * thisParserInstance = this;
    

    That’s initialised the first time you call the function, and then left unchanged on future calls. So if you call the function on one object, destroy that object, and then call it on a second object, you’ll actually be working on a dangling pointer to the defunct object. That will certainly give undefined behaviour.

    There’s no reason to use a static variable; the lambdas can capture this and act on the correct object. Or more simply, as suggested in the comments, use the variadic form of async to bind this to the member function:

    std::async(std::launch::async, &parser::parseSentences, this);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first attempt at using a resource file. I have seen a
this if my first attempt at using streaming for WCF, and I am struggling
I'm writing an application using DDD techniques. This is my first attempt at a
Because this is my first attempt at an extension method that seems quite useful
Up front: This is my first attempt at an Android app. I'm in that
This is my first attempt at using BufferStrategy and I'd really appreciate some hints.
Although I am not new to Python, this is my first attempt at using
This is my first attempt at using Github's shared collaboration model to work on
I currently have a fairly straight forward app, my first attempt at MVVM using
This is my first attempt at using Backbone.js, so I decided to make a

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.