std::queue memory leak

Started by
3 comments, last by Jezper 12 years, 11 months ago
I'm trying to implement a log message system for a 3d engine, it's using std::queue to store the messages.
But i'm getting this wierd memory leak message from CrtDbg, the code looks completley safe tome, so need
some fresh eyes on this,

I'll show you a test program i wrote to demonstrate the leak:

#include <iostream>
#include <string>
#include <queue>

#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>

#define MYDEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__ )
#define new MYDEBUG_NEW

class LogMessage
{
public:
LogMessage() : level(0), timeStamp(0), text() { }
LogMessage(int level, int timeStamp, const std::string &text) : level(level), timeStamp(timeStamp), text(text) { }
~LogMessage() {}

public:
std::string text;
int timeStamp;
int level;
};

int main()
{
std::queue<LogMessage> q;

q.push(LogMessage(0, 0, "leaky"));
q.pop();

_CrtDumpMemoryLeaks();
return 0;
}


And the leak message:

Detected memory leaks!
Dumping objects ->
{139} normal block at 0x001A4B50, 40 bytes long.
Data: < eaky > 00 00 00 00 00 65 61 6B 79 00 CD CD CD CD CD CD
{138} normal block at 0x001A4AF0, 32 bytes long.
Data: <PK > 50 4B 1A 00 00 00 00 00 00 00 00 00 00 00 00 00
{135} normal block at 0x001A4A18, 8 bytes long.
Data: < 5 > D8 F7 35 00 00 00 00 00
Object dump complete.
Advertisement
First try:int main()
{
{
std::queue<LogMessage> q;

q.push(LogMessage(0, 0, "leaky"));
q.pop();
}

_CrtDumpMemoryLeaks();
return 0;
}


The "leak" may also be unrelated to your code, the main preamble allocates some static data that is often falsely reported as a leak.

First try:int main()
{
{
std::queue<LogMessage> q;

q.push(LogMessage(0, 0, "leaky"));
q.pop();
}

_CrtDumpMemoryLeaks();
return 0;
}


The "leak" may also be unrelated to your code, the main preamble allocates some static data that is often falsely reported as a leak.


Thanks, adding brackets did work!
So i guess i must have some problems with the destucion of the class i'm using it then, wich is also wierd, couse i tried setting a breakpoint in the destructor to make sure it's called, and it is.
Guess i'll have to investigate it futher...
Or rather simply the destruction of the queue itself, which is done earlier with the brackets.
Finally fixxed it. And it was a "false" leak.
Had a class declared in main wich wasn't destructed untill the end ('}')
and i called _CrtDumpMemoryLeaks() before it's destructor.

Thank's again Antheus, i added brackets in the code to forced destruction earlier and it works like a charm.

This topic is closed to new replies.

Advertisement