分段故障失败?

时间:2011-11-15 23:15:20

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

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();
    }
}

2 个答案:

答案 0 :(得分:1)

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

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

不要忘记在完成后关闭它:

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

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

答案 1 :(得分:-1)

从你的删除测试(这是完全不必要的BTW,NULL的删除是完全定义的)我收集connection等可能是NULL。但是,在使用它们之前,请不要检查NULL。因此,其中一个可能是NULL,因此您的访问会产生分段错误。

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

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

这些是我看到的可能性。只有你展示的代码没有办法说更多。您真的应该使用调试器单步执行它,并查看分段错误发生的位置。

相关问题