简体   繁体   English

C ++线程中的分段错误

[英]Segmentation fault in C++ thread

I'm trying to set up a basic threaded class in C++, but I'm getting a seg fault when I try to create a thread. 我试图在C ++中建立一个基本的线程类,但是当我尝试创建线程时却遇到了段错误。 Here's what GDB reports: 以下是GDB报告的内容:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000401b68 in StartThread (pFunction=
    0x401ad2 <FindPrimesThread(void*)>, pLimit=5000000) at Thread.cpp:35
35          state->mLimit = pLimit;

when I try to call it like this: 当我尝试这样称呼它时:

ThreadState *primesState = StartThread(FindPrimesThread, 5000000);

Here's my code: 这是我的代码:

Thread.hpp 线程.hpp

#ifndef THREAD_HPP
#define THREAD_HPP

#include <pthread.h>
#include "Types.hpp"

typedef struct {
    ulong       mLimit;     // Upper limit of numbers to test 
    int         mStarted;   // True if the thread started successfully
    int         mExitCode;  // Thread exit code
    pthread_t   mThreadId;  // Thread ID
} ThreadState;

// Defines a type named ThreadFunction which is a pointer to a function with void * as the parameter and
// void * as the return value.
typedef void *(*ThreadFunction)(void *);

ThreadState *StartThread
    (
    ThreadFunction const pFunction,  // Pointer to the thread function
    ulong const          pLimit      // Upper limit of numbers to test
    );

#endif

Thread.cpp 线程.cpp

#include "Amicable.hpp"
#include "Keith.hpp"
#include "Main.hpp"
#include "Prime.hpp"
#include "Thread.hpp"

ThreadState *StartThread
    (
    ThreadFunction const pFunction,  // Pointer to the thread function
    ulong const          pLimit      // Upper limit of numbers to test
    ) {
        ThreadState *state;
        state->mLimit = pLimit;
        pthread_t threadId;
        state->mStarted = pthread_create(&threadId, NULL, pFunction, (void *)state);
        if(state->mStarted == 0){
            state->mThreadId = threadId;
        }
        return state;
    }

Any idea on what's going wrong here? 对这里出什么问题有任何想法吗?

ThreadState *state;
state->mLimit = pLimit;

You are writing to memory you haven't allocated 您正在写入尚未分配的内存

You have an uninitialized pointer in ThreadState. 您在ThreadState中有一个未初始化的指针。 On line 35, you create a pointer to a ThreadState, but you never assign that pointer to point to any ThreadState object. 在第35行,您创建了一个指向ThreadState的指针,但从未分配该指针指向任何ThreadState对象。

Remember, a pointer is just a memory address. 记住,指针只是一个内存地址。 "ThreadState*" just means, "this is a memory address, and we can interpret the data in memory at the address I hold to be a ThreadState object." “ ThreadState *”仅表示“这是一个内存地址,我们可以将内存中的数据解释为我持有的作为ThreadState对象的地址。”

Perhaps you meant to do "ThreadState *state = new ThreadState();"? 也许您打算执行“ ThreadState * state = new ThreadState();”? Don't forget, someone will need to go and delete that ThreadState object when they're done using it so as not to leak memory! 别忘了,有人在使用完该线程状态对象后将需要删除它,以免泄漏内存!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM