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?