|
-
April 10th, 2004, 05:41 PM
#1
Memory Leaks deleting ptr to TCHAR[]'s inside CPtrArray
Hi
I'm trying to store many TCHAR strings as dynamically created on the heap as pointers inside a CArray (CTypedPtrArray<CPtrArray,TCHAR*>).
I made a small class to wrap it all as below. However when I come to clean up after deleting the class it reports each string as a leak, even though I did the delete on it.
Any Ideas?
Header
Code:
class CTextContainerArray : public CTypedPtrArray<CPtrArray,TCHAR*>
{
// Construction
public:
CTextContainerArray();
~CTextContainerArray();
void AddString(TCHAR* pStr,int len);
};
Body
Code:
CTextContainerArray::CTextContainerArray(){}
CTextContainerArray::~CTextContainerArray()
{
TCHAR* pText;
for (int j =0; j < GetSize(); j++)
{
pText = (TCHAR*)GetAt(j);
if(pText)
{
delete [] pText;
}
}
RemoveAll();
}
void CTextContainerArray::AddString(TCHAR* pStr,int len)
{
TCHAR* newstr = NULL;
if ((len > 0) && pStr)
{
newstr = new TCHAR[len];
if (newstr)
{
wcscpy(newstr,pStr);
}
}
Add(newstr);
}
Sample of Memory leak output:
{9904} normal block at 0x011AD6E8, 64 bytes long.
Data: < C| > B8 AE 43 7C 17 00 00 00 17 00 00 00 01 00 00 00
{9903} normal block at 0x011AD688, 30 bytes long.
Data: < C| > B8 AE 43 7C 06 00 00 00 06 00 00 00 01 00 00 00
{9902} normal block at 0x011AD608, 64 bytes long.
Data: < C| > B8 AE 43 7C 17 00 00 00 17 00 00 00 01 00 00 00
{9901} normal block at 0x011AD5A8, 30 bytes long.
Something else that was quite interesting, is that previous to the version above. I allocated the memory for the string separately, copied the string into the new memory location given by new. Then used the CArray->Add member to simply add the pointer to the allocated string. This also leaked. But the leak was the actual string memory location. IE you could see the text in the leak print out!
Do I need to write my own heap manager functions and use my own heap area? Or am I doing something really silly?
Also does it matter that this class is created in a worker thread of a CDoculent? The class is then added to another CArray. I haven't had any problems with these.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|