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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:18:43+00:00 2026-06-15T00:18:43+00:00

Header file: // Free function to use in thread unsigned int __stdcall WorkerFunction(void *);

  • 0

Header file:

// Free function to use in thread
unsigned int __stdcall WorkerFunction(void *);

class MyClass {
    public:
        int temp;
        void StartThread();
}

typedef struct {
    MyClass * cls;
} DATA;

CPP class:

void MyClass::StartThread() {
    temp = 1234;
    DATA data = {this};

    HANDLE hThread = (HANDLE) _beginthreadex(0, 0, &WorkerFunction, &data, 0, 0);

    // Commented out for now while I address the current problem
    //CloseHandle(hThread);
}

unsigned int __stdcall WorkerFunction(void * param0) {
    MessageBox(NULL, "WorkerFunction()", "Alert", MB_OK);
    DATA * data = (DATA *) param0;
    MyClass* cls0 = data->cls;

    // Crashes when reference to cls0 is attempted.
    char buf[5];
    snprintf(buf, 5, "%i", cls0 ->temp);
    MessageBox(NULL, buf, "Alert", MB_OK);
}

I’ve got an easy problem here that I can’t put my finger on.

  • I have params for a thread, which pass a struct which contains a class.
  • I instantiate the struct with this and then pass it when the thread starts
  • I try to dereference (?) it in the worker function.
  • At this point, everything compiles OK.
  • When I add lines to access something in the class, the app crashes.

Where is my mistake?

  • 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-15T00:18:45+00:00Added an answer on June 15, 2026 at 12:18 am

    You’re passing a local stack variable that is out-of-scope the moment StartThread() returns. You are therefore referencing stack-space that no longer belongs to you.

    void MyClass::StartThread() {
        temp = 1234;
        DATA data = {this}; // << LOCAL VARIABLE OUT OF SCOPE ON FUNCTION EXIT
    
        HANDLE hThread = (HANDLE) _beginthreadex(0, 0, &WorkerFunction, &data, 0, 0);
    
        // Commented out for now while I address the current problem
        //CloseHandle(hThread);
    }
    

    Either dynamically allocate the data, or better still, make the data a member of MyClass and pass this as the thread data. In your case you’re only passing *this anyway through a struct, so just pass it as the param

    void MyClass::StartThread() {
        temp = 1234;
    
        HANDLE hThread = (HANDLE) _beginthreadex(0, 0, &WorkerFunction, this, 0, 0);
    
        // Commented out for now while I address the current problem
        //CloseHandle(hThread);
    }
    

    And in your thread proc:

    unsigned int __stdcall WorkerFunction(void * param0) {
        MessageBox(NULL, "WorkerFunction()", "Alert", MB_OK);
        MyClass* cls0 = static_cast<MyClass*>(param0);
    
       etc...
    }
    

    Whatever you pass to your thread procedure, it must have valid lifetime for the duration required. by the thread. Either ensure that by passing ownership of a dynamic allocation to the thread at let it do the delete, or pass a pointer known hold determinate state for the lifetime of its usage in the thread-proc. It appears this fulfills the latter, so you should likely be good to go.

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

Sidebar

Related Questions

header file: private: vector<int*>* nums; public slots: void buttonClicked(); cpp file: NewWindow(){ int one
i have shared the header file containing class definition of a Context Free grammar
I'm trying to use a non-standard header file (http://ndevilla.free.fr/gnuplot). Its used in lots of
I've defined a C++ class with the following header file: class EarleyParser { public:
Header file: // pe10-8arr.h -- header file for a simple list class #ifndef SIMPLEST_
in header file I have defined the following function #ifndef OS_H #define OS_H #include
Consider having the following header file (c++): myclass.hpp #ifndef MYCLASSHPP_ #define MYCLASSHPP_ namespace A
Initializing in the header file i get the following error: invalid in-class initialization of
I've been trying to make a basic XOR header file for use in some
I have a header file buildTree.h and a C file buildTree.c there is 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.