pengxiqin 发表于 2009-10-27 00:14:23

释放ACE_Message_Block出错,为什么?谢谢

在下面的代码中,为什么在int QTest::DequeueAllMsgs()函数里调用mb->release();的时候出现内存释放错误!谢谢!代码如下:

文件MQEG1.h
#ifndef __MQ_EG1_H__
#define __MQ_EG1_H__

#include "ace/Null_Condition.h"
#include "ace/Null_Mutex.h"
#include "ace/Message_Queue.h"
#include "ace/Log_Msg.h"
#include "ace/OS.h"

class QTest
{
public:
    // Constructor creates a message queue with asynchronism
    QTest( int numMsgs );

    // Enqueue the number of messages required onto the message queue
    int EnqueueMsgs();

    // Dequeue all the messages previously enqueued.
    int DequeueAllMsgs();

private:
    // Underlying message queue
    ACE_Message_Queue< ACE_NULL_SYNCH > *mq_;
    // Number of message to enqueue
    int numMsgs_;
};

#endif // __MQ_EG1_H__

文件MQEG1.cpp

#include "MQEG1.h"

QTest:QTest( int numMsgs )
      : numMsgs_(numMsgs)
{
    // Create a message queue of default size.
    if ( !(mq_ = new ACE_Message_Queue< ACE_NULL_SYNCH > ()) )
    {
      ACE_DEBUG( (LM_ERROR,"Error in message queue initialization\n") );
    }
}

int QTest::EnqueueMsgs()
{
    ACE_TRACE("QTest::EnqueueMsgs\n");

    for ( int i = 0; i < numMsgs_; i++ )
    {
      // Create a new message block specifying exactly
      // an underlying data block should be created
      ACE_Message_Block *mb;
      ACE_NEW_RETURN( mb, ACE_Message_Block(ACE_OS::strlen("This is message 1\n")), -1 );

       // Insert data into the message block using wr_ptr
       ACE_OS::sprintf( mb->wr_ptr(), "This is message %d\n", i );

       // Be careful to advance the wr_ptr by the necessary amount.
       mb->wr_ptr(ACE_OS::strlen("This is message 1\n"));

       // Enqueue the message block onto the message queue
       if ( this->mq_->enqueue_prio(mb) == -1 )
       {
            ACE_ERROR( (LM_ERROR, "Could not enqueue on to message queue\n") );
            return -1;
       }

      ACE_DEBUG( (LM_INFO,"EQ'd data: %s\n", mb->rd_ptr()) );
    }// end for

    // Dequeue all the message
    DequeueAllMsgs();

    return 0;
}

int QTest:DequeueAllMsgs()
{
    ACE_TRACE( "QTest:DequeueAllMsgs\n" );

    ACE_DEBUG( (LM_INFO, "No. of messages on Q:%d Bytes on Q:%d \n", mq_->message_count(), mq_->message_bytes()) );

    ACE_Message_Block *mb;

    // Dequeue the head of the message queue until no more messages are left.
    for ( int i = 0; i < numMsgs_; i++ )
    {
      mq_->dequeue_head( mb );
      ACE_DEBUG((LM_INFO,"DQ'd data: %s\n", mb->rd_ptr() ));

      // Release the memory associated with the message block
      mb->release();
    }

    return 0;
}


int main( int argc, char *argv[] )
{
    QTest q(5);
    q.EnqueueMsgs();

    return 0;
}

在用Release释放ACE_Message_Block的时候弹出对话框,提示如下错误。
Debug Error!
Program D:\Ace_Practice\Sample1.exe
DAMAGE: after Normal block(#153) at 0x003FC938

我用的编译器是VC2003.

[ 本帖最后由 pengxiqin 于 2009-10-28 00:45 编辑 ]

wishel 发表于 2009-10-28 14:44:16

ACE_NEW_RETURN( mb, ACE_Message_Block(ACE_OS::strlen("This is message 1\n") + 1), -1 );
别忘记最后的'\0',做c字串处理的时候要始终提醒自己,尤其是调用到系统的字串处理函数时。

pengxiqin 发表于 2009-10-28 19:12:00

回复 #2 wishel 的帖子

谢谢版主。:handshake

winston 发表于 2009-10-28 21:53:51

wishel 经验老道。有水平和经验的程序员,会自觉的这么干。
页: [1]
查看完整版本: 释放ACE_Message_Block出错,为什么?谢谢