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

I have the following code which works on the .txt file
...
Can anyone please help me how can i avoid the memory leaks here

Frankly, I don't see how it may be freed at all. Judging from the code, you build a list of chunks, and besides that each chunk is referenced from keyword. The keywords themselves, as far as I can tell, are only accessible through the chunks. Is it what you have intended?
If so, don't call it memory leak. If not, describe your intended data structure design and we can figure something out.

Frankly, I don't see how it may be freed at all. Judging from the code, you build a list of chunks, and besides that each chunk is referenced from keyword. The keywords themselves, as far as I can tell, are only accessible through the chunks. Is it what you have intended?
If so, don't call it memory leak. If not, describe your intended data structure design and we can figure something out.

Hi,
Thanks for your reply, actually this code is not written by me, instead i was asked to fix the memory leaks. infact i found few memory leaks When i ran the application using third party leak detector tools like Visual Leak Detector and GlowCode etc.

Below is the structure for your reference

typedef struct ExportDevlib TKCBLChunk
{
	TKCBLChunk (void)		{ keywordList.KillAll (); parent = NULL;	}
	~TKCBLChunk (void)		{ keywordList.KillAll (); parent = NULL;	}
	CLList		keywordList;
	TKCBLChunk*	parent;				// This chunks parent. Null if top chunk	
} TKCBLChunk;

// This structure *MUST* be (sizeof(TKCBLKeyword)%4 == 0). Add pads to it if it isnt!
typedef struct TKCBLKeyword
{
	TKCBLKeyword (void)		
	{ keyword = NULL; dataStrings = NULL; subchunk = NULL; numDataStrings = 0; totalStringSize = 0;	}
	~TKCBLKeyword (void)
	{ keyword = NULL; dataStrings = NULL; subchunk = NULL; numDataStrings = 0; totalStringSize = 0;	}
	char*		keyword;
	char**		dataStrings;
	xlong		numDataStrings, totalStringSize;
	TKCBLChunk*	subchunk;
} TKCBLKeyword;
This question has already been answered. Start a new discussion instead.