Sign in to follow this  
qbicpp

std::queue memory leak

Recommended Posts

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:
[code]
#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;
}
[/code]

And the leak message:
[code]
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.
[/code]

Share this post


Link to post
Share on other sites
First try:[code]int main()
{
{
std::queue<LogMessage> q;

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

_CrtDumpMemoryLeaks();
return 0;
}[/code]

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

Share this post


Link to post
Share on other sites
[quote name='Antheus' timestamp='1305920375' post='4813593']
First try:[code]int main()
{
{
std::queue<LogMessage> q;

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

_CrtDumpMemoryLeaks();
return 0;
}[/code]

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

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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this