使用 activemq-cpp 库创建新线程期间的分段错误问题

Posted

技术标签:

【中文标题】使用 activemq-cpp 库创建新线程期间的分段错误问题【英文标题】:Segmentation fault issue during creation new thread using activemq-cpp library 【发布时间】:2016-04-02 12:35:09 【问题描述】:

我的环境是opensuse、gcc5、clion、active-mq 3.10.0。 我错过了什么吗?启动程序后出现分段错误。

    class MyTask : public CompositeTask


    public:
        MyTask()  

    public:
        virtual bool isPending() const override 
            cout<<"MyTask()::isPending\n";
            return true;
        

        virtual bool iterate() override 
            cout<<"MyTask()::iterate\n";
            return false;
        


    ;


    class RR
        CompositeTaskRunner compositeTaskRunner;
    public:
        RR()


              CompositeTask* m = new MyTask();
                compositeTaskRunner.addTask(m);
            
            void start()
                compositeTaskRunner.start();

            


        ;

main()

      RR* runner = new RR();
      runner->start();


valgrind 的输出

==12202== Memcheck, a memory error detector
==12202== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==12202== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==12202== Command: ./CppMicroServicesExampleDriver
==12202== Parent PID: 9602
==12202== 
==12202== Invalid read of size 8
==12202==    at 0x5BDCD29: createThreadInstance (Threading.cpp:365)
==12202==    by 0x5BDCD29: decaf::internal::util::concurrent::Threading::createNewThread(decaf::lang::Thread*, char const*, long long) (Threading.cpp:920)
==12202==    by 0x5C21BB3: decaf::lang::Thread::initializeSelf(decaf::lang::Runnable*, std::string const&, long long) (Thread.cpp:123)
==12202==    by 0x5C21F7A: decaf::lang::Thread::Thread(decaf::lang::Runnable*, std::string const&) (Thread.cpp:102)
==12202==    by 0x5A5AC36: activemq::threads::CompositeTaskRunner::CompositeTaskRunner() (CompositeTaskRunner.cpp:70)
==12202==    by 0x40F5E1: RR::RR() (nano.hpp:32)
==12202==    by 0x40BEAD: main (main.cpp:56)
==12202==  Address 0x60 is not stack'd, malloc'd or (recently) free'd
==12202== 
==12202== 
==12202== Process terminating with default action of signal 11 (SIGSEGV)
==12202==  Access not within mapped region at address 0x60
==12202==    at 0x5BDCD29: createThreadInstance (Threading.cpp:365)
==12202==    by 0x5BDCD29: decaf::internal::util::concurrent::Threading::createNewThread(decaf::lang::Thread*, char const*, long long) (Threading.cpp:920)
==12202==    by 0x5C21BB3: decaf::lang::Thread::initializeSelf(decaf::lang::Runnable*, std::string const&, long long) (Thread.cpp:123)
==12202==    by 0x5C21F7A: decaf::lang::Thread::Thread(decaf::lang::Runnable*, std::string const&) (Thread.cpp:102)
==12202==    by 0x5A5AC36: activemq::threads::CompositeTaskRunner::CompositeTaskRunner() (CompositeTaskRunner.cpp:70)
==12202==    by 0x40F5E1: RR::RR() (nano.hpp:32)
==12202==    by 0x40BEAD: main (main.cpp:56)
==12202==  If you believe this happened as a result of a stack
==12202==  overflow in your program's main thread (unlikely but
==12202==  possible), you can try to increase the size of the
==12202==  main thread stack using the --main-stacksize= flag.
==12202==  The main thread stack size used in this run was 8388608.
==12202== 
==12202== HEAP SUMMARY:
==12202==     in use at exit: 126,122 bytes in 577 blocks
==12202==   total heap usage: 1,031 allocs, 454 frees, 152,249 bytes allocated
==12202== 
==12202== LEAK SUMMARY:
==12202==    definitely lost: 0 bytes in 0 blocks
==12202==    indirectly lost: 0 bytes in 0 blocks
==12202==      possibly lost: 0 bytes in 0 blocks
==12202==    still reachable: 126,122 bytes in 577 blocks
==12202==                       of which reachable via heuristic:
==12202==                         stdstring          : 15,204 bytes in 453 blocks
==12202==         suppressed: 0 bytes in 0 blocks
==12202== Rerun with --leak-check=full to see details of leaked memory
==12202== 
==12202== For counts of detected and suppressed errors, rerun with: -v
==12202== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

【问题讨论】:

【参考方案1】:

在使用该库之前,您必须调用其初始化方法,以便准备管理资源所需的所有内部构造。

    activemq::library::ActiveMQCPP::initializeLibrary();

【讨论】:

以上是关于使用 activemq-cpp 库创建新线程期间的分段错误问题的主要内容,如果未能解决你的问题,请参考以下文章

静态链接 ActiveMQ-cpp

ActiveMQ-cpp:尽管发生故障转移,但连接丢失

多线程编程之Windows环境下创建新线程

Java 线程池使用详解

activemq-cpp pull consumer 接收远程地址

创建新应用程序和库时出现@nrwl/nx 错误