I have the following code which works on the .txt file
The file content is as given below.
;*
;*
{ = LanguageIso
LANGUAGE_ISO = "EN"
}
xlong MyProg::Import(const char* buffer, xlong x)
{
try
{
while (1)
{
if ((bytesParsed = AddLine(tmpSrcBuffer)) == -1)
return FALSE;
tmpSrcBuffer += bytesParsed;
line++;
// Jump over new line chars
if (*tmpSrcBuffer == 0xd) tmpSrcBuffer++;
if (*tmpSrcBuffer == 0xa) tmpSrcBuffer++;
if (*tmpSrcBuffer == 0)
break;
}
}
catch(...) {
g_log.Error("CKCBL::Import: Failed at %100.100s (the error might be much earlier)",buffer );
throw;
}
}
Below is the Code that reads each line and inserts into the List. when the string '{' is matched
the if block is executed.
xlong MyProg::AddLine(const char* rawStr)
{
// Setup memory ( +3 so we are sure we have room for the terminating characters)
TKCBLKeyword* newKeyword = NULL;
const size_t allocSize = sizeof(TKCBLKeyword) + rsData.size + (rsData.numData * sizeof(xlong*) + 3);
CHK_NEW (newKeyword = (TKCBLKeyword*) new char[allocSize])
if (newKeyword == NULL)
return -1;
xMemset(newKeyword, 0, sizeof(TKCBLKeyword));
newKeyword->totalStringSize = rsData.size;
newKeyword->numDataStrings = rsData.numData;
newKeyword->dataStrings = (char**) ((xlong)newKeyword + sizeof(TKCBLKeyword));
dataStringsDst = (char*) ((xlong)newKeyword->dataStrings + (rsData.numData * sizeof(xlong*)));
rsData.dstStr = dataStringsDst;
//Add it
curChunk->keywordList.InsertLast(newKeyword);
if (!xStricmp(newKeyword->keyword, "{"))
{
TKCBLChunk* newChunk = NULL;
CHK_NEW (newChunk = new TKCBLChunk) // TODO: We are leaking memory here...
if (newChunk == NULL)
return -1;
newKeyword->subchunk = newChunk;
newChunk->parent = curChunk;
curChunk = newChunk;
}
return rsData.bytesParsed;
}
Here is where I am having problems, the memory leaks. the newChunk is not free'd.
for your reference, I also tried adding CHK_DELETE(newChunk) at line number 32, but that results in
"Unhandled exception at 0x1029e844 (lib.dll) in Application.exe: 0xC0000005: Access violation reading location 0x00000011
the exception is raised at line number 23 in the below code
/***************************************************************************
* Insert item last in list
*/
xword CLList::InsertLast(void* iPtr, xlong type)
{
CNode* newNode = (CNode*) iPtr;
if (type == LL_DATA)
newNode = List_AllocNode (); //GlobalAlloc(GMEM_SHARE,sizeof(CNode));
else
iPtr = newNode->itemPointer;
if (newNode == NULL)
return 0;
// newNode->Init();
if (tailNode != NULL)
{
newNode->nextNode = NULL;
newNode->prevNode = tailNode;
newNode->itemPointer = iPtr;
tailNode->nextNode = newNode;
tailNode = newNode;
}
else
{
newNode->nextNode = NULL;
newNode->prevNode = NULL;
newNode->itemPointer = iPtr;
headNode = newNode;
tailNode = newNode;
currNode = newNode;
}
numnodes++;
return 1;
}
Can anyone please help me how can i avoid the memory leaks here