Memory leaking

S

Sigmathaar

Hi, I'm having some trouble while debuging some code. My compiler says
the code is OK but whenever I try to execute the program nothing
happens. Using my debuger I got the next message :

Detected memory leaks!
Dumping objects ->
{56} normal block at 0x02EA5DF8, 6 bytes long.
Data: <U> 50 75 65 62 6C 61
{55} normal block at 0x02EA5DB0, 7 bytes long.
Data: <O> 4E 61 79 61 72 69 74
{54} normal block at 0x02EA5D60, 16 bytes long.
Data: <I> 45 73 74 61 64 6F 20 64 65 20 4D E9 78 69 63 6F
{53} normal block at 0x02EA5D10, 16 bytes long.
Data: <E> 44 69 73 74 72 69 74 6F 20 46 65 64 65 72 61 6C
{52} normal block at 0x02EA5CC0, 19 bytes long.
Data: <A> 42 61 6A 61 20 43 61 6C 69 66 6F 72 6E 69 61 20
{51} normal block at 0x02EA5C80, 1 bytes long.
Data: <Vowels> 46
Object dump complete.

This is the code I'm using, it's some kind of XML paser which has beed
adapted from an MSDN example of the DOM library :

*********************************************** CODE
***********************************************

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <windows.h>
#include <direct.h>
#import <msxml3.dll> raw_interfaces_only

char * BSTRtoChar(BSTR bstr)
{
// CP_ACP for a Windows ANSI conversion
unsigned long length = WideCharToMultiByte (
CP_ACP, 0, bstr, SysStringLen(bstr), NULL, 0, NULL, NULL);

char * convStr= new char[length];

length = WideCharToMultiByte ( CP_ACP, 0, bstr, SysStringLen(bstr),
reinterpret_cast <char *>(convStr), length, NULL, NULL);

convStr[length] = '\0';
return convStr;
}

// Macro that calls a COM method returning HRESULT value:
#define HRCALL(a, errmsg) \
do { \
hr = (a); \
if (FAILED(hr)) { \
dprintf( "%s:%d HRCALL Failed: %s\n 0x%.8x = %s\n", \
__FILE__, __LINE__, errmsg, hr, #a ); \
goto clean; \
} \
} while (0)

// Helper function that put output in stdout and debug window
// in Visual Studio:
void dprintf( char * format, ...)
{
static char buf[1024];
va_list args;
va_start( args, format );
vsprintf( buf, format, args );
va_end( args);
OutputDebugStringA( buf);
printf("%s", buf);
}

// Helper function to create a DOM instance:
IXMLDOMDocument * DomFromCOM()
{
HRESULT hr;
IXMLDOMDocument *pxmldoc = NULL;

HRCALL( CoCreateInstance(__uuidof(MSXML2::DOMDocument30),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(IXMLDOMDocument),
(void**)&pxmldoc),
"Create a new DOMDocument");

HRCALL( pxmldoc->put_async(VARIANT_FALSE),
"should never fail");
HRCALL( pxmldoc->put_validateOnParse(VARIANT_FALSE),
"should never fail");
HRCALL( pxmldoc->put_resolveExternals(VARIANT_FALSE),
"should never fail");

return pxmldoc;
clean:
if (pxmldoc)
{
pxmldoc->Release();
}
return NULL;
}

VARIANT VariantString(BSTR str)
{
VARIANT var;
VariantInit(&var);
V_BSTR(&var) = SysAllocString(str);
V_VT(&var) = VT_BSTR;
return var;
}

void ReportParseError(IXMLDOMDocument *pDom, char *desc) {
IXMLDOMParseError *pXMLErr=NULL;
BSTR bstrReason = NULL;
HRESULT hr;
HRCALL(pDom->get_parseError(&pXMLErr),
"dom->get_parseError: ");
HRCALL(pXMLErr->get_reason(&bstrReason),
"parseError->get_reason: ");

dprintf("%s %S\n",desc, bstrReason);
clean:
if (pXMLErr) pXMLErr->Release();
if (bstrReason) SysFreeString(bstrReason);
}

int _tmain(int argc, _TCHAR* argv[])
{
IXMLDOMDocument *pXMLDom=NULL;
IXMLDOMNodeList *pNodes=NULL;
IXMLDOMNode *pNode=NULL;
BSTR bstr = NULL;
VARIANT_BOOL status;
VARIANT var;
HRESULT hr;
long length;
CHAR szRoot[18];

CoInitialize(NULL);

pXMLDom = DomFromCOM();
if (!pXMLDom) goto clean;

VariantInit(&var);
var = VariantString(L"00000.xml");
HRCALL(pXMLDom->load(var, &status), "dom->load(): ");
if (status!=VARIANT_TRUE) {
ReportParseError(pXMLDom,
"Failed to load DOM from stocks.xml");
goto clean;
}

// Query a single node.
if (bstr) SysFreeString(bstr);
bstr = SysAllocString(L"//thesaurusList/thesaurusEntry/@lang");
HRCALL(pXMLDom->selectSingleNode(bstr,
&pNode),"dom->selectSingleNode: ");
if (!pNode) {
ReportParseError(pXMLDom, "Calling selectSingleNode ");
}
else {
HRCALL(pNode->get_text(&bstr)," get_text ");
sprintf(szRoot, "%s\\%s\\%s", "C:", "cdWebFolder", BSTRtoChar(bstr));
}


// Query a node-set.
if (bstr) SysFreeString(bstr);
bstr = SysAllocString(L"//thesaurusEntry/topTerm");
HRCALL(pXMLDom->selectNodes(bstr, &pNodes), "selectNodes ");
if (!pNodes) {
ReportParseError(pXMLDom, "Error while calling selectNodes ");
}
else {
HRCALL(pNodes->get_length(&length), "get_length: ");
for (long i=0; i<length; i++) {
if (pNode) pNode->Release();
HRCALL(pNodes->get_item(i, &pNode), "get_item: ");
if (bstr) SysFreeString(bstr);
HRCALL(pNode->get_text(&bstr), "get_text: ");
CHAR szPath[50];
sprintf(szPath, "%s\\%s\\", szRoot, BSTRtoChar(bstr));
_mkdir(szPath);
}
}

clean:
if (bstr) SysFreeString(bstr);
if (&var) VariantClear(&var);
if (pXMLDom) pXMLDom->Release();
if (pNodes) pNodes->Release();
if (pNode) pNode->Release();

CoUninitialize();
return 0;
}

****************************************** END OF CODE
******************************************

Can somebody explain why there is a memory leaking in this code?

Thanks.
 
J

Jim Langston

Sigmathaar said:
Hi, I'm having some trouble while debuging some code. My compiler says
the code is OK but whenever I try to execute the program nothing
happens. Using my debuger I got the next message :

Detected memory leaks!
Dumping objects ->
{56} normal block at 0x02EA5DF8, 6 bytes long.
Data: <U> 50 75 65 62 6C 61
{55} normal block at 0x02EA5DB0, 7 bytes long.
Data: <O> 4E 61 79 61 72 69 74
{54} normal block at 0x02EA5D60, 16 bytes long.
Data: <I> 45 73 74 61 64 6F 20 64 65 20 4D E9 78 69 63 6F
{53} normal block at 0x02EA5D10, 16 bytes long.
Data: <E> 44 69 73 74 72 69 74 6F 20 46 65 64 65 72 61 6C
{52} normal block at 0x02EA5CC0, 19 bytes long.
Data: <A> 42 61 6A 61 20 43 61 6C 69 66 6F 72 6E 69 61 20
{51} normal block at 0x02EA5C80, 1 bytes long.
Data: <Vowels> 46
Object dump complete.

This is the code I'm using, it's some kind of XML paser which has beed
adapted from an MSDN example of the DOM library :

I would suggest you post this question in microsoft.public.vc.language.
There is heavy Windows code in this and without knowing what the windows
specific functions do it'll be hard to find the problem.
 
Y

yuvalif

BSTRtoChar allocate memory but you call it without assigning its
allocated pointer into anything, that a memory leak.
 
S

Sigmathaar

yuva said :
BSTRtoChar allocate memory but you call it without assigning its
allocated pointer into anything, that a memory leak.

I've tried to do something like this :

char * conversion;
conversion = BSTRtoChar(bstr);
sprintf(szPath, "%s\\%s\\", szRoot, conversion);

If doing so the message in the debuger is still the same :

Detected memory leaks! ....

I don't know if that's what you were saying but if not can you please
be more clear?
 
D

deane_gavin

Sigmathaar said:
yuva said :


I've tried to do something like this :

char * conversion;
conversion = BSTRtoChar(bstr);
sprintf(szPath, "%s\\%s\\", szRoot, conversion);

If doing so the message in the debuger is still the same :

Detected memory leaks! ....

I don't know if that's what you were saying but if not can you please
be more clear?

I hope not, since (as has been pointed out) BSTRtoChar is a Windows API
function so has nothing to do with the C++ language.

If you ask in a Windows programming newsgroup I am sure you will find
people who can help you. Moreover, any answers you get will be
peer-reviewed by other Windows API experts who will be able to point
out any corrections necessary in inaccurate answers. You won't get that
level of confidence here.

Gavin Deane
 
?

=?ISO-8859-1?Q?Stefan_N=E4we?=

Sigmathaar said:
Hi, I'm having some trouble while debuging some code. My compiler says
the code is OK but whenever I try to execute the program nothing
happens. Using my debuger I got the next message :

Detected memory leaks!
Dumping objects ->
{56} normal block at 0x02EA5DF8, 6 bytes long.
Data: <U> 50 75 65 62 6C 61
{55} normal block at 0x02EA5DB0, 7 bytes long.
Data: <O> 4E 61 79 61 72 69 74
{54} normal block at 0x02EA5D60, 16 bytes long.
Data: <I> 45 73 74 61 64 6F 20 64 65 20 4D E9 78 69 63 6F
{53} normal block at 0x02EA5D10, 16 bytes long.
Data: <E> 44 69 73 74 72 69 74 6F 20 46 65 64 65 72 61 6C
{52} normal block at 0x02EA5CC0, 19 bytes long.
Data: <A> 42 61 6A 61 20 43 61 6C 69 66 6F 72 6E 69 61 20
{51} normal block at 0x02EA5C80, 1 bytes long.
Data: <Vowels> 46
Object dump complete.

This is the code I'm using, it's some kind of XML paser which has beed
adapted from an MSDN example of the DOM library :

*********************************************** CODE
***********************************************

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <windows.h>
#include <direct.h>
#import <msxml3.dll> raw_interfaces_only

char * BSTRtoChar(BSTR bstr)
{
// CP_ACP for a Windows ANSI conversion
unsigned long length = WideCharToMultiByte (
CP_ACP, 0, bstr, SysStringLen(bstr), NULL, 0, NULL, NULL);

char * convStr= new char[length];

length = WideCharToMultiByte ( CP_ACP, 0, bstr, SysStringLen(bstr),
reinterpret_cast <char *>(convStr), length, NULL, NULL);

convStr[length] = '\0';
return convStr;
}

// Macro that calls a COM method returning HRESULT value:
#define HRCALL(a, errmsg) \
do { \
hr = (a); \
if (FAILED(hr)) { \
dprintf( "%s:%d HRCALL Failed: %s\n 0x%.8x = %s\n", \
__FILE__, __LINE__, errmsg, hr, #a ); \
goto clean; \
} \
} while (0)

// Helper function that put output in stdout and debug window
// in Visual Studio:
void dprintf( char * format, ...)
{
static char buf[1024];
va_list args;
va_start( args, format );
vsprintf( buf, format, args );
va_end( args);
OutputDebugStringA( buf);
printf("%s", buf);
}

// Helper function to create a DOM instance:
IXMLDOMDocument * DomFromCOM()
{
HRESULT hr;
IXMLDOMDocument *pxmldoc = NULL;

HRCALL( CoCreateInstance(__uuidof(MSXML2::DOMDocument30),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(IXMLDOMDocument),
(void**)&pxmldoc),
"Create a new DOMDocument");

HRCALL( pxmldoc->put_async(VARIANT_FALSE),
"should never fail");
HRCALL( pxmldoc->put_validateOnParse(VARIANT_FALSE),
"should never fail");
HRCALL( pxmldoc->put_resolveExternals(VARIANT_FALSE),
"should never fail");

return pxmldoc;
clean:
if (pxmldoc)
{
pxmldoc->Release();
}
return NULL;
}

VARIANT VariantString(BSTR str)
{
VARIANT var;
VariantInit(&var);
V_BSTR(&var) = SysAllocString(str);
V_VT(&var) = VT_BSTR;
return var;
}

void ReportParseError(IXMLDOMDocument *pDom, char *desc) {
IXMLDOMParseError *pXMLErr=NULL;
BSTR bstrReason = NULL;
HRESULT hr;
HRCALL(pDom->get_parseError(&pXMLErr),
"dom->get_parseError: ");
HRCALL(pXMLErr->get_reason(&bstrReason),
"parseError->get_reason: ");

dprintf("%s %S\n",desc, bstrReason);
clean:
if (pXMLErr) pXMLErr->Release();
if (bstrReason) SysFreeString(bstrReason);
}

int _tmain(int argc, _TCHAR* argv[])
{
IXMLDOMDocument *pXMLDom=NULL;
IXMLDOMNodeList *pNodes=NULL;
IXMLDOMNode *pNode=NULL;
BSTR bstr = NULL;
VARIANT_BOOL status;
VARIANT var;
HRESULT hr;
long length;
CHAR szRoot[18];

CoInitialize(NULL);

pXMLDom = DomFromCOM();
if (!pXMLDom) goto clean;

VariantInit(&var);
var = VariantString(L"00000.xml");
HRCALL(pXMLDom->load(var, &status), "dom->load(): ");
if (status!=VARIANT_TRUE) {
ReportParseError(pXMLDom,
"Failed to load DOM from stocks.xml");
goto clean;
}

// Query a single node.
if (bstr) SysFreeString(bstr);
bstr = SysAllocString(L"//thesaurusList/thesaurusEntry/@lang");
HRCALL(pXMLDom->selectSingleNode(bstr,
&pNode),"dom->selectSingleNode: ");
if (!pNode) {
ReportParseError(pXMLDom, "Calling selectSingleNode ");
}
else {
HRCALL(pNode->get_text(&bstr)," get_text ");
sprintf(szRoot, "%s\\%s\\%s", "C:", "cdWebFolder", BSTRtoChar(bstr));
}


// Query a node-set.
if (bstr) SysFreeString(bstr);
bstr = SysAllocString(L"//thesaurusEntry/topTerm");
HRCALL(pXMLDom->selectNodes(bstr, &pNodes), "selectNodes ");
if (!pNodes) {
ReportParseError(pXMLDom, "Error while calling selectNodes ");
}
else {
HRCALL(pNodes->get_length(&length), "get_length: ");
for (long i=0; i<length; i++) {
if (pNode) pNode->Release();
HRCALL(pNodes->get_item(i, &pNode), "get_item: ");
if (bstr) SysFreeString(bstr);
HRCALL(pNode->get_text(&bstr), "get_text: ");
CHAR szPath[50];
sprintf(szPath, "%s\\%s\\", szRoot, BSTRtoChar(bstr));
This call --------------------------------------------------^^^^^^^^^

allocates something with 'new' which doesn't get deleted.

Try:
HRCALL(pNode->get_text(&bstr), "get_text: ");
CHAR szPath[50];
char* p = BSTRtoChar(bstr);
sprintf(szPath, "%s\\%s\\", szRoot, p);
delete[] p;
_mkdir(szPath);
}
}

clean:
if (bstr) SysFreeString(bstr);
if (&var) VariantClear(&var);
if (pXMLDom) pXMLDom->Release();
if (pNodes) pNodes->Release();
if (pNode) pNode->Release();

CoUninitialize();
return 0;
}

HTH

Stefan
 
A

Axter

Sigmathaar said:
Hi, I'm having some trouble while debuging some code. My compiler says
the code is OK but whenever I try to execute the program nothing
happens. Using my debuger I got the next message :

Detected memory leaks!
Dumping objects ->
{56} normal block at 0x02EA5DF8, 6 bytes long.
Data: <U> 50 75 65 62 6C 61
{55} normal block at 0x02EA5DB0, 7 bytes long.
Data: <O> 4E 61 79 61 72 69 74
{54} normal block at 0x02EA5D60, 16 bytes long.
Data: <I> 45 73 74 61 64 6F 20 64 65 20 4D E9 78 69 63 6F
{53} normal block at 0x02EA5D10, 16 bytes long.
Data: <E> 44 69 73 74 72 69 74 6F 20 46 65 64 65 72 61 6C
{52} normal block at 0x02EA5CC0, 19 bytes long.
Data: <A> 42 61 6A 61 20 43 61 6C 69 66 6F 72 6E 69 61 20
{51} normal block at 0x02EA5C80, 1 bytes long.
Data: <Vowels> 46
Object dump complete.

This is the code I'm using, it's some kind of XML paser which has beed
adapted from an MSDN example of the DOM library :

*********************************************** CODE
***********************************************

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <windows.h>
#include <direct.h>
#import <msxml3.dll> raw_interfaces_only

char * BSTRtoChar(BSTR bstr)
{
// CP_ACP for a Windows ANSI conversion
unsigned long length = WideCharToMultiByte (
CP_ACP, 0, bstr, SysStringLen(bstr), NULL, 0, NULL, NULL);

char * convStr= new char[length];

length = WideCharToMultiByte ( CP_ACP, 0, bstr, SysStringLen(bstr),
reinterpret_cast <char *>(convStr), length, NULL, NULL);

convStr[length] = '\0';
return convStr;
}

// Macro that calls a COM method returning HRESULT value:
#define HRCALL(a, errmsg) \
do { \
hr = (a); \
if (FAILED(hr)) { \
dprintf( "%s:%d HRCALL Failed: %s\n 0x%.8x = %s\n", \
__FILE__, __LINE__, errmsg, hr, #a ); \
goto clean; \
} \
} while (0)

// Helper function that put output in stdout and debug window
// in Visual Studio:
void dprintf( char * format, ...)
{
static char buf[1024];
va_list args;
va_start( args, format );
vsprintf( buf, format, args );
va_end( args);
OutputDebugStringA( buf);
printf("%s", buf);
}

// Helper function to create a DOM instance:
IXMLDOMDocument * DomFromCOM()
{
HRESULT hr;
IXMLDOMDocument *pxmldoc = NULL;

HRCALL( CoCreateInstance(__uuidof(MSXML2::DOMDocument30),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(IXMLDOMDocument),
(void**)&pxmldoc),
"Create a new DOMDocument");

HRCALL( pxmldoc->put_async(VARIANT_FALSE),
"should never fail");
HRCALL( pxmldoc->put_validateOnParse(VARIANT_FALSE),
"should never fail");
HRCALL( pxmldoc->put_resolveExternals(VARIANT_FALSE),
"should never fail");

return pxmldoc;
clean:
if (pxmldoc)
{
pxmldoc->Release();
}
return NULL;
}

VARIANT VariantString(BSTR str)
{
VARIANT var;
VariantInit(&var);
V_BSTR(&var) = SysAllocString(str);
V_VT(&var) = VT_BSTR;
return var;
}

void ReportParseError(IXMLDOMDocument *pDom, char *desc) {
IXMLDOMParseError *pXMLErr=NULL;
BSTR bstrReason = NULL;
HRESULT hr;
HRCALL(pDom->get_parseError(&pXMLErr),
"dom->get_parseError: ");
HRCALL(pXMLErr->get_reason(&bstrReason),
"parseError->get_reason: ");

dprintf("%s %S\n",desc, bstrReason);
clean:
if (pXMLErr) pXMLErr->Release();
if (bstrReason) SysFreeString(bstrReason);
}

int _tmain(int argc, _TCHAR* argv[])
{
IXMLDOMDocument *pXMLDom=NULL;
IXMLDOMNodeList *pNodes=NULL;
IXMLDOMNode *pNode=NULL;
BSTR bstr = NULL;
VARIANT_BOOL status;
VARIANT var;
HRESULT hr;
long length;
CHAR szRoot[18];

CoInitialize(NULL);

pXMLDom = DomFromCOM();
if (!pXMLDom) goto clean;

VariantInit(&var);
var = VariantString(L"00000.xml");
HRCALL(pXMLDom->load(var, &status), "dom->load(): ");
if (status!=VARIANT_TRUE) {
ReportParseError(pXMLDom,
"Failed to load DOM from stocks.xml");
goto clean;
}

// Query a single node.
if (bstr) SysFreeString(bstr);
bstr = SysAllocString(L"//thesaurusList/thesaurusEntry/@lang");
HRCALL(pXMLDom->selectSingleNode(bstr,
&pNode),"dom->selectSingleNode: ");
if (!pNode) {
ReportParseError(pXMLDom, "Calling selectSingleNode ");
}
else {
HRCALL(pNode->get_text(&bstr)," get_text ");
sprintf(szRoot, "%s\\%s\\%s", "C:", "cdWebFolder", BSTRtoChar(bstr));
}


// Query a node-set.
if (bstr) SysFreeString(bstr);
bstr = SysAllocString(L"//thesaurusEntry/topTerm");
HRCALL(pXMLDom->selectNodes(bstr, &pNodes), "selectNodes ");
if (!pNodes) {
ReportParseError(pXMLDom, "Error while calling selectNodes ");
}
else {
HRCALL(pNodes->get_length(&length), "get_length: ");
for (long i=0; i<length; i++) {
if (pNode) pNode->Release();
HRCALL(pNodes->get_item(i, &pNode), "get_item: ");
if (bstr) SysFreeString(bstr);
HRCALL(pNode->get_text(&bstr), "get_text: ");
CHAR szPath[50];
sprintf(szPath, "%s\\%s\\", szRoot, BSTRtoChar(bstr));
_mkdir(szPath);
}
}

clean:
if (bstr) SysFreeString(bstr);
if (&var) VariantClear(&var);
if (pXMLDom) pXMLDom->Release();
if (pNodes) pNodes->Release();
if (pNode) pNode->Release();

CoUninitialize();
return 0;
}

****************************************** END OF CODE
******************************************

Can somebody explain why there is a memory leaking in this code?

Thanks.

Consider using the code in the following link, which can give you
better details about your memory leak:
http://code.axter.com/leaktracker.h

http://code.axter.com/leaktracker.zip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top