PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

stl::vector leak?

hi guys,
has anyone experienced this... ?

i checked my app for leaks using crtdbg methods.
it seems std::vector (that came with vc++) has a leak.

the debugger dumps and detects ... something like this

{1707} normal block at 0x00945030, 1 bytes long.
Data: < > 00

and i traced it back to the push_back method.

i coded a simple linked list in replacement of the vector class i used and no leak was detected.

but a leak of 1 bytes long? sounds something else is wrong.
is it just me? or has anyone else experience this?
I've been using different implementation of std::vector<> for 5 years and I've never seen one leak.

Perhaps you could post some code we can look at?
ok, i've recreated the leak in a small program.
my code has COM-like interfaces or abstract layers.

the vector leak (if indeed it is from the vector) seems to come when it is used together with the object abstract...

just see below...


Code:

#include <windows.h>
#include <tchar.h>

#include <vector>
#include <iostream>

#include <stdlib.h>
#include <crtdbg.h>

// tracer
inline void _cdecl DbgTrace(LPCTSTR lpszFormat, ...)
{
va_list args;
va_start(args, lpszFormat);
int nBuf;
TCHAR szBuffer[512];
nBuf = _vsntprintf(szBuffer, sizeof(szBuffer), lpszFormat, args);
OutputDebugString(szBuffer);
va_end(args);
}

#define TRACE(i) DbgTrace(_T("%d\n"),i)
#define TRACEF(f) DbgTrace(_T("%f\n"),f)
#define TRACES(s) DbgTrace(_T("%s\n"),s)

//-----------------
// COM-like Interface
//-----------------

class IObject
{
public:
virtual void Initialize()=0;
virtual void Shutdown()=0;
};

class IFactory
{
public:
virtual bool Create(int nId,void**)=0;
virtual void Destroy(void*)=0;
};

int g_items=0;
class MyItem {
public:
MyItem() {
g_items++;
}
~MyItem() {
g_items--;
TRACE(g_items);
// note all items have been properly freed
}
int m_x;
int m_y;
};

typedef std::vector<MyItem*> items_t;

//-----------------
// MyObject
//-----------------
class MyObject : public IObject
{
public:
void Initialize() {
for(int i=0;i<10;i++) {
MyItem *item=new MyItem();
m_items.push_back(item);
//m_items[i]=item;
}
}
void Shutdown() {
for(int i=0;i<10;i++) {
MyItem *item=m_items[i];
delete item;
}
m_items.clear();
}
items_t m_items;
//MyItem* m_items[10]; // change vector to a simple array... fixes the leak
};

//-----------------
// MyFactory
//-----------------
class MyFactory : public IFactory
{
public:
// create an object
bool Create(int nId,void** ptrObject) {
if (nId==0) {
MyObject *obj=new MyObject();
IObject *iobj=reinterpret_cast<IObject*>(obj);
iobj->Initialize();
*ptrObject=iobj;
return 1;
}
return 0;
}
// destroy
void Destroy(void* ptrObject) {
//---------
// leak source!!!
// produces a leak whenever iobj has a vector container
//---------
IObject *iobj=reinterpret_cast<IObject*>(ptrObject);
iobj->Shutdown();
delete iobj;

//---------
// change above to below... also fixes the leak
// but then, it renders my IObject interface useless...
// because IObject could be... MyOtherObject
//---------
//MyObject *obj=reinterpret_cast<MyObject*>(ptrObject);
//obj->Shutdown();
//delete obj;
}
};

int main()
{
_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);

//-----------
// These codes don't leak....
//-----------
TCHAR *x=new TCHAR[123];
delete x;

MyObject *obj=new MyObject();
obj->Initialize();
obj->Shutdown();
delete obj;

//-----------
// Here's the code that leaks
//-----------
MyFactory factory;
IObject *iobj;
if (factory.Create(0,(void**)&iobj)) {
// ... do something with the obj
factory.Destroy(iobj);
}

return 0;
}


disclaimer: i'm not a COM programmer, i just implemented my COM-like architecture from my own little understanding, and some readings. if flaws... comment please

the app i'm working on needs to have a plugin-capable architecture. i want my dll's to be able to access each other and even the base executable file. that's why... the above complexity... any better approach?
angedelamort · 30 Mar 2004, 14:19 UTC
TCHAR *x=new TCHAR[123];
delete x;

In my memory, when I'm using the new [] operator, I think we are supposed to use delete [] operator. So I suspect that there is a leak in that.


For the rest, everything looks fine.
Pejo Software - Per · 30 Mar 2004, 14:30 UTC
Yes you shall use the delete[] but I don't think that this will result in a leak. It just means that destructors won't be called but since TCHAR don't have a destructor...

I also had a quick look at the code and could not find anything that would result in a leak.

You could test to see if there is any difference if you use STLPort instead.
I believe behaviour is undefined if you use new[] but not delete[].
I believe behaviour is undefined if you use new[] but not delete[].


And this could explain your leak. Can you test this before we all get a headache reading your code? =)


Ouch! You use reinterepret_cast<> ! This is plain evil! When you need here is static_cast<>. That could also explain your problems.
Pejo Software - Per · 30 Mar 2004, 20:06 UTC
Add:
virtual ~IObject() {}
to your IObject class and your memory leak will dissapear!
Pejo Software - Per wrote:Add:
virtual ~IObject() {}
to your IObject class and your memory leak will dissapear!


this did it!!! leak fixed :)
thanks pejo.
but could you offer a reason why this solves the leak? where's the leak, in MyObject or in vector?

... delete[] ... and delete for TCHAR didn't matter >> no leak
Thierry wrote:
Ouch! You use reinterepret_cast<> ! This is plain evil! When you need here is static_cast<>. That could also explain your problems.


thanks for the suggestion...
i'll have to review the difference :)
STLPort 4.5.3
also leaked...
even with Pejo's ... virtual ~IObject() {};

is there a method other than clear() that frees up everything vector allocates?

or guess i'll move on and use a primitive linked list :(
i'll probably save a few kbs from not creating many vector templates
How about you fix the reinterpret_cast<> and the delete [] problems before blaming STLport? Or did you fix them?

You don't have anything to do to clear a vector, it will take care of itself.
yes, i removed the tchar[] delete.. and used static_cast as you suggested.

it resulted in two leaks

1304 bytes long and 320 bytes long,

i had a _CrtSetBreakAlloc(45) ... this one is inside the push_back method


note:
vc++ only produced one leak 64 bytes long...
i used a simple array instead of vector, no leak is produced...

i'll check other ways to reproduce the leak without reinterpret_cast or static_cast
here it is... no casting...

stlport 4.5.3
produces two leaks 1304 and 320 bytes long

note:
vc++'s stl doesn't produce leak

Code:

#define _STLP_NEW_PLATFORM_SDK

#include <windows.h>
#include <vector>

#include <stdlib.h>
#include <crtdbg.h>

//-----------------
// COM-like Interface
//-----------------

class IObject
{
public:
virtual void Initialize()=0;
virtual void Shutdown()=0;
};

class MyItem {
public:
MyItem() {
}
~MyItem() {
}
int m_x;
int m_y;
};

typedef std::vector<MyItem*> items_t;

//-----------------
// MyObject
//-----------------
class MyObject : public IObject
{
public:
void Initialize() {
for(int i=0;i<10;i++) {
MyItem *item=new MyItem();
m_items.push_back(item);
//m_items[i]=item;
}
}
void Shutdown() {
for(int i=0;i<10;i++) {
MyItem *item=m_items[i];
delete item;
}
m_items.clear();
}
items_t m_items;
//MyItem* m_items[10]; // change vector to a simple array... fixes the leak
};

int main()
{
_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
//_CrtSetBreakAlloc(45);

//-----------
// These leaks....
//-----------
MyObject *obj=new MyObject();
obj->Initialize();
obj->Shutdown();
delete obj;

return 0;
}
There might be some interaction problem between STLport's node allocator and the memory leak detector.

Try disabling the node allocator and see what it does. It's somewhere in stl_config I think, check their web site for more info.
Pejo Software - Per · 31 Mar 2004, 06:14 UTC
izeman":14rj7l8j]

If you don't write a destructor in a class the compiler would use a destructor with the following form:
~IObject() {}
When you then have a pointer to a IObject and delete it, only that constructor will be called. if you add virtual the destructor of the derived class will be called.

Regarding the last code you posted I could not find anything that would result in a leak so I tested it (on Windows) and it did not leak!
pejo,
the code i posted leaked only with STLPORT 4.5.3...

this solved the leak...

#define _STLP_DEBUG_ALLOC
(thanks to thierry)

the allocators from crtdbg probably messes up with stl...
i wish i had boundschecker or one of those debuggers

... thanks for the help guys
Pejo, isn't the compiler-generated destructor also virtual due to the presence of other virtual functions in the class?
Pejo Software - Per · 31 Mar 2004, 15:51 UTC
I have never heard that it would be, but I am not one of those that can reel off the standard.
Also here we have an evidence that it doesn't, the leak was removed by adding a virtual destructor.

However since the class is Abstract (pure virtual) it would make sense that the destructor always would be virtual. But I have no influence on the compiler so I gues it isn't up to me.
mlepage wrote:Pejo, isn't the compiler-generated destructor also virtual due to the presence of other virtual functions in the class?


Absolutely no. There is no link whatsoever. But a virtual destructor in a base class will make all destructors in derived classes virtual. But that's only common sense.
izeman wrote:pejo,
the code i posted leaked only with STLPORT 4.5.3...

this solved the leak...

#define _STLP_DEBUG_ALLOC
(thanks to thierry)

the allocators from crtdbg probably messes up with stl...
i wish i had boundschecker or one of those debuggers

... thanks for the help guys


Glad it worked. BoundChecker is actually worst when it comes to STLport and keeps reporting non-existing leaks/access violations. At least that was my experience with it about a year ago.

The node allocators aren't actually leaking, they are just wrongly accused of doing so. (Which can be annoying when you are trying to find real leaks.)
Thierry":1kqqcz9g]

OK, double check and you're right, what I was remembering was the "rule" that any class that has a virtual function should have a virtual destructor, and that the implicitly declared destructor would be virtual if the base class destructor was.

Pejo: Beware that what your compiler does it not necessarily evidence of what is correct! Or should we strike covariant return types from the standard because VC6 doesn't support them?