【问题标题】:Segmentation Fault Fail?分段故障失败?
【发布时间】:2015-07-02 05:49:07
【问题描述】:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -177935456 (LWP 5483)]
0xf79ff2ca in activemq::core::ActiveMQSessionExecutor::dispatch (this=0xf4b04bc0, 
    dispatch=@0xf564e240) at activemq/core/ActiveMQSessionExecutor.cpp:129
129 activemq/core/ActiveMQSessionExecutor.cpp: No such file or directory.
    in activemq/core/ActiveMQSessionExecutor.cpp
Current language:  auto; currently c++

我该如何解决这个问题?你需要更多代码吗?我不知道它在哪里失败?我怎样才能找到它失败的地方?

它转储到哪里?

编辑:
这是代码:

std::string ActiveMQWrapper::get(){
    Connection* connection;
    Session* session;
    Destination* destination;
    MessageConsumer* consumer;

    try {
        std:string brokerURI = "tcp://localhost:61613?wireFormat=stomp";
        auto_ptr<ConnectionFactory> connectionFactory(ConnectionFactory::createCMSConnectionFactory( brokerURI ) );
        connection = connectionFactory->createConnection();
        connection->start();

        session = connection->createSession( Session::AUTO_ACKNOWLEDGE );
        destination = session->createQueue( "TEST.Prototype" );
        consumer = session->createConsumer( destination );
        TextMessage* textMessage =
            dynamic_cast< TextMessage* >( consumer->receive() );

        string text = "";

        if( textMessage != NULL ) {
            text = textMessage->getText();
        } else {
            text = "NOT A TEXTMESSAGE!";
        }

        try{
            if( destination != NULL ) delete destination;
        }catch (CMSException& e) { e.printStackTrace(); }
        destination = NULL;

        try{
            if( consumer != NULL ) delete consumer;
        }catch (CMSException& e) { e.printStackTrace(); }
        consumer = NULL;

        // Close open resources.
        try{
            if( session != NULL ) session->close();
            if( connection != NULL ) connection->close();
        }catch (CMSException& e) { e.printStackTrace(); }

        // Now Destroy them
        try{
            if( session != NULL ) delete session;
        }catch (CMSException& e) { e.printStackTrace(); }
        session = NULL;

        try{
            if( connection != NULL ) delete connection;
        }catch (CMSException& e) { e.printStackTrace(); }
        connection = NULL;

         return text.c_str();

    } catch( CMSException& e ) {
        e.printStackTrace();
    }
}

【问题讨论】:

  • 是的,我们需要更多代码。一个好的 IDE 和调试器可以显示堆栈跟踪。需要注意的一些事情是一个错误,只是尝试访问未分配的内存。
  • IDE 是 VIM,没有 gdb 是调试器。
  • 你在抓什么?这些析构函数中的任何一个实际上会抛出CMSException吗?
  • 我没有析构函数。我把一个类变成了一个函数。所以我必须清理没有?
  • @DarthVader :他的意思是任何抛出的析构函数都被破坏了,所以将你的deletes 包裹在try..catch 块中暗示了关于Connection 实现的非常糟糕的事情, SessionDestination

标签: c++ segmentation-fault activemq activemq-cpp


【解决方案1】:

我在寻找此问题的答案时偶然发现了这一点,并找到了正确的解决方案。 ActiveMQ-CPP库需要先正确初始化:

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

完成后别忘了关闭它:

activemq::library::ActiveMQCPP::shutdownLibrary();

它实际上是 OP 链接到的网页的一部分: http://activemq.apache.org/cms/example.html

【讨论】:

    【解决方案2】:

    根据您对删除的测试(顺便说一句,这是完全不必要的,NULL 上的删除是完全定义的)我认为connection 等可能是 NULL。但是,在使用它们之前,您不会在上面检查 NULL。因此,其中一个可能是 NULL,因此您的访问会导致分段错误。

    另外:ConnectionFactory::createCMSConnectionFactory 返回的指针是否分配了new?因为否则将它们存储在 auto_ptr 中是不正确的做法。

    此外,在您实例化 auto_ptr 时,类型 ConnectionFactory 是否已定义(而不是仅(前向)声明)?因为在不完整的类型上实例化auto_ptr(例如仅声明但尚未定义的类型)是未定义的行为,也可能导致分段错误。

    这些是我看到的可能性。仅凭您显示的代码无法说更多。您确实应该使用调试器单步执行它,看看出现分段错误的位置。

    【讨论】:

    • 好吧,由于我不知道库的接口,我无法确定您是否应该删除;对于connectiondestinationsessionuser,我很确定你应该这样做。但是,要完全确定您需要查看文档(指针不需要 指向使用new 分配的内存)。但是完全没有必要在删除之前检查 NULL(即,而不是 it (foo!=NULL) delete foo;,只写 delete foo; 而不测试 NULL)。但是,在 使用这些指针之前,您应该测试 NULL(但不要)。
    猜你喜欢
    • 2018-01-19
    • 1970-01-01
    • 2016-09-09
    相关资源
    最近更新 更多