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:OMDocument30),
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.
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:OMDocument30),
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.