|
在下面的代码中,为什么在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 编辑 ] |
|