|
-
July 26th, 2007, 11:05 AM
#1
[RESOLVED] COleVariant and strings
Hello,
I have a class which encapsulates a COleVariant. Here's a brief outlook of the class:
Code:
CEncaps::CEncaps() :
m_pVariant(new COleVariant())
{
}
CEncaps::~CEncaps(void)
{
if (m_pVariant)
{
delete m_pVariant;
m_pVariant = NULL;
}
}
BOOLEAN CEncaps::Read(CFile& file)
{
BOOLEAN bOK = TRUE;
switch (m_enumType)
{
case TYPE_BYTE:
{
m_pVariant->vt = VT_UI1;
file.Read(&(m_pVariant->bVal), sizeof(BYTE));
}
break;
// ...
// All other numeric types are handled similarly to bytes
// ...
case TYPE_STRING:
{
char* pBuf = new char[m_nSize + 1];
file.Read(pBuf, m_nSize);
pBuf[m_nSize] = '\0';
m_pVariant->SetString(pBuf, VT_BSTR);
}
break;
}
return bOK;
}
BOOLEAN CEncaps::Write(CFile& file)
{
BOOLEAN bOK = TRUE;
switch (m_enumType)
{
case TYPE_BYTE:
{
file.Write(&(m_pVariant->bVal), sizeof(BYTE));
}
break;
// ...
// All other numeric types are handled similarly to bytes
// ...
case TYPE_STRING:
{
BSTR bstr = m_pVariant->bstrVal;
CString cstr(bstr);
LPTSTR pBuf = cstr.GetBufferSetLength(m_nSize);
file.Write(pBuf, m_nSize);
}
break;
default:
break;
}
return bOK;
}
(m_enumType and m_nSize are set by other methods not shown here for the sake of clarity.)
Now, my problem: this code works but produces memory leaks. I'm almost certain the leaks are caused only by strings. I find the BSTR type very confusing! I think I'm supposed to call SysFreeString() at some point, but my tries have been unsuccessful (produce error in the destructor).
So now I'm asking you code gurus I'd be really grateful if someone can help me correct this leak. Thanks in advance!
Last edited by wlof; July 27th, 2007 at 07:41 AM.
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
|