2011-12-02 130 views
16

我有一個非常糟糕的內存泄漏,我試圖修復,但不知何故,我無法刪除對象,而沒有觸發這個斷言。調試斷言失敗... _BLOCK_TYPE_IS_VALID(pHead-> nBlockUse)

我已經通過Google搜索了一個解決方案,並閱讀了有關此錯誤的stackoverflow問題,但我仍然無法找到答案!

可能的原因,根據我的研究,出現此錯誤:
1.刪除對象不止一個
2.影子複製
3.創建和刪除是從外部的dll加載的對象
4.創建對象不存儲指針

但是:
1.我檢查了代碼,沒能找到雙缺失
2.我用一個拷貝構造函數來複制對象
3.錯誤關聯類是建立(與MS Visual Studio)到一個單獨的庫,但不是一個DLL。並且與此錯誤相關的所有類都位於同一個庫中。
4.我檢查了代碼,它似乎不是問題

如果有人能夠發現下面的代碼中的錯誤,這將是非常好的,我感謝每一個提示,指出我的解決方案問題。

編輯:
我忘了提及MessageSystem的sendThreadMain(請參閱下面的代碼)中相同的刪除問題。如果我在那裏刪除消息,它會在代碼中的其他地方導致意外的錯誤。可能只是錯誤的數據傳輸......但我不知道。
此代碼在Windows和Linux上運行!

下面是相關的部分代碼錯誤:

消息

class Message 
{ 
public: 
    Message (char type, unsigned char id, unsigned short size) 
    { 
     mType = type; 
     mId = id; 
     mSize= size; 
    } 

    Message(const Message &o) 
    { 
     mType = o.mType; 
     mId = o.mId; 
     mSize = o.mSize; 
    } 

    char getType() const {return mType;}; 
    unsigned char getId() const {return mId;}; 
    unsigned short getSize() const {return mSize;}; 

protected: 
    char mType; 
    unsigned char mId; 
    unsigned short mSize; 
}; 


class JoinMessage : public Message 
{ 
public: 
    JoinMessage() : Message ('j', 0, sizeof (JoinMessage)) 
    { 
     team = TEAM_SPECTATOR; 
    } 
    JoinMessage (unsigned char id) : Message ('j', id, sizeof (JoinMessage)){} 
    JoinMessage (const JoinMessage &o) : Message (o) 
    { 
     team = o.team; 
     setName(o.getName()); 
    } 


    void setName(std::string newName) 
    { 
     if (newName.length() > MAX_PLAYER_NAME_LENGHT) 
      newName = newName.substr(0, MAX_PLAYER_NAME_LENGHT); 

     memset(name, 0, MAX_PLAYER_NAME_LENGHT); 
     for(unsigned int i = 0; i < newName.length(); i++) 
      name[i] = newName[i]; 
    } 

    std::string getName() const 
    { 
     std::string stringToReturn; 

     for(unsigned int i = 0; i < MAX_PLAYER_NAME_LENGHT; i++) 
     { 
      if (name[i]) 
       stringToReturn.push_back(name[i]); 
      else 
       break; 
     } 

     return stringToReturn; 
    } 

    TeamIdentifier team; 

private: 
    unsigned char name[MAX_PLAYER_NAME_LENGHT]; 
}; 

// there are a lot other messages 

的MessageQueue

MessageQueue::~MessageQueue() 
{ 
    boost::mutex::scoped_lock lock (queueMutex); 

    while(messageQueue.size() > 0) 
    { 
     // the crash is non-reproducible 
     // works 90% of the time 
     delete messageQueue.front(); // <- Debug Assertion Failed … _BLOCK_TYPE_IS_VALID 
     messageQueue.pop_front(); 
    } 

} 

void MessageQueue::enqueMessage (Message* message) 
{ 
    { 
     boost::mutex::scoped_lock lock (queueMutex); 
     messageQueue.push_back(message); 
    } 
} 

Message* MessageQueue::dequeMessage() 
{ 
    boost::mutex::scoped_lock lock (queueMutex); 
    if (messageQueue.size() == 0) 
     return nullptr; 

    Message* message = messageQueue.front(); 
    messageQueue.pop_front(); 

    return message; 
} 

MessageSystem

template <class MessageType> 
void broadcast (MessageType &message) 
{ 
    MessageType *internMessage = new MessageType(message); 

    boost::mutex::scoped_lock lock (mRecipientMapMutex); 
    std::map <boost::asio::ip::udp::endpoint, MessageQueue *>::iterator it; 

    for (it = mRecipientMap.begin(); 
     it != mRecipientMap.end(); 
     it++) 
    { 
     it->second->enqueMessage(internMessage); 

    } 
} 


template <class MessageType> 
void post (MessageType &message, boost::asio::ip::udp::endpoint &recipient) 
{ 
    MessageType *internMessage = new MessageType(message); 

    std::map <boost::asio::ip::udp::endpoint, MessageQueue* >::iterator it; 
    MessageQueue *messageQueue = NULL; 
    { 
     boost::mutex::scoped_lock lock (mRecipientMapMutex); 
     it = mRecipientMap.find (recipient); 
     if (it != mRecipientMap.end()) 
      messageQueue = it->second; 

     if(messageQueue) 
      messageQueue->enqueMessage (internMessage); 
    } 

} 


void MessageSystem::sendThreadMain() 
{ 
    // copy endpoints to vecotr so it can be 
    // deleted from map while iterating 
    std::vector<udp::endpoint> endpoints; 
    { 
     boost::mutex::scoped_lock lock (mRecipientMapMutex); 
     std::map <udp::endpoint, MessageQueue *>::iterator mapIt = mRecipientMap.begin(); 
     while (mapIt != mRecipientMap.end()) 
     { 
      endpoints.push_back(mapIt->first); 
      mapIt++; 
     } 
    } 

    std::vector<udp::endpoint>::iterator endpointIt = endpoints.begin(); 
     while (endpointIt != endpoints.end()) 
     { 
      char sendBuffer[PACKET_SIZE]; 
      int sendBufferPosition = 0; 
      { 
       boost::mutex::scoped_lock lock (mRecipientMapMutex); 

       MessageQueue *messageQueue = mRecipientMap[*endpointIt]; 
       if (messageQueue == nullptr) 
       { 
        mRecipientMap.erase(*endpointIt); 
        endpointIt++; 
        continue; 
       } 

       while (Message *message = messageQueue->dequeMessage()) 
       { 
        if (sendBufferPosition + message->getSize() > PACKET_SIZE) 
        { 
         // put message back and send it later 
         messageQueue->enqueMessage (message); 
         break; 
        } 

        // copy message into buffer 
        std::memcpy (
         &sendBuffer [sendBufferPosition], message, message->getSize()); 

        sendBufferPosition += message->getSize(); 
        // deleting this message causes a crash if 2 or more 
        // recipients are registered within MessageSystem 
        //delete message; <- RANDOM CRASH elsewhere in the program 
       } 
      } 
    .... // more code down here that seems not related to the error 
+0

您不會將消息添加到除'broadcast'功能以外的任何其他位置的隊列嗎? –

+0

有好消息和壞消息。好消息是,這絕對不是由內存泄漏造成的。 –

+0

我不確定這是否是問題,但是您應該在類Message中擁有虛擬析構函數。 – 2011-12-02 10:35:22

回答

3

今天我想到了我自己。這是問題中提到的4種可能性中的第一種。

  1. 刪除對象,曾多次(通過保存多個指針,以完全相同的對象)

這是我在的MessageQueue解決方法:

template <class MessageType> 
void broadcast (MessageType &message) 
{ 
    // I was creating 1 new Message right here but I need 1 new Message 
    // in EVERY MessageQueue so i moved the next line ... 
    // MessageType *internMessage = new MessageType(message); 

    boost::mutex::scoped_lock lock (mRecipientMapMutex); 
    std::map <boost::asio::ip::udp::endpoint, MessageQueue *>::iterator it; 

    for (it = mRecipientMap.begin(); 
     it != mRecipientMap.end(); 
     it++) 
    { 
     // ... down here. Now every queue contains its own copy of the Message 
     MessageType *internMessage = new MessageType(message); 
     it->second->enqueMessage(internMessage); 
    } 
} 
1

這可能是一個簡單的親錯誤順序的瑕疵。你正在做的:

while(messageQueue.size() > 0) 
{ 
    delete messageQueue.front(); 
    messageQueue.pop_front(); 
} 

也許刪除消息彈出之後,而不是之前,會做的伎倆:

while(messageQueue.size() > 0) 
{ 
    Message* pFront = messageQueue.front(); 
    messageQueue.pop_front(); 
    delete pFront; 
} 

反正我根本沒有信心在這個解決方案,因爲刪除由pFront指向的對象應該對隊列本身沒有影響,它只存儲指針。但你可以試試。

+1

我也認爲它應該對隊列沒有任何影響,但我會盡力去做。但它似乎是另一個問題,因爲從隊列中刪除消息後刪除消息不起作用(請參閱MessageSystem末尾的while循環) – cwin

1

好吧,我面臨着類似的問題, 以下代碼

Message* message = messageQueue.front(); 
messageQueue.pop_front(); 

return message; 

The與我產生的錯誤代碼是:

Point *p = q.LookFor(&q, &pts[5], &Dist); 
cout ... 
delete p; 

看來這個函數刪除它會在運行時的指針,所以你不能刪除「再次」

,所以我用它代替

Point p = *(q.LookFor(&q, &pts[5], &Dist)); 

它消失了。