Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Memory leak in GetInput() function of MSComm in VC++

Status
Not open for further replies.

dennis24

Programmer
Dec 8, 2003
5
0
0
MY
Please help!!!

Has anyone noticed that there is a memory leak in GetInput() function of MSComm? Consider the following code (assume that connection already existed and is working fine):

COleVariant ovData;
for (int i=0; i<500000; i++)
{
ovData = m_mcComm1.GetInput();
}

I noticed that everytime when I executed the above code, my PC's memory usage will increase about 10 MB (I got the memory usage information from Windows Task Manager).

So, after several round of testing, I decided to add a new line of code after GetInput() to free the memory occupied:

COleVariant ovData;
for (int i=0; i<500000; i++)
{
ovData = m_mcComm1.GetInput();
SysFreeString(ovData.bstrVal);
}

This help to reduce the memory leak, but it still happen (increase about 4K - 10K everytime the code is executed).

I wonder is it something to do with my code, or I have missed out some important setting for MSComm, or is it a bug in GetInput() function?

Your help is very much appreciated. Many thanks.
 
Try ovData.Clear instead of SysFreeString

Ion Filipski
1c.bmp
 
Hi Ion Filipski

Thanks for your reply. I have tried that but it was the same - caused memory leak about 10MB. Any other ideas? Thanks.
 
what kind(type) of data does GetInput return?

Ion Filipski
1c.bmp
 
I think I've understood the problem. The opeartor = copy the value into CVariant. So, after each iteration is allocated some memory and created a copy in the variant. When clear, you clear only the copy inside variant. So, you should destroy also the original copy.

Ion Filipski
1c.bmp
 
Thanks Ion Filipski. But how do I clear the original copy of the variant? I don't have the source code for MSComm.

Anyway, I have gave up MSComm and switched to CSerialPort, a freeware MFC class to wrap access to the Win32 APIs dealing with serial ports, written by PJ Naughter. You can have a lot at
It is easy to use and most importantly, does not causes memory leak!!!

Thanks again for your concern on this matter.
 
I see you use SysFreeString to free memory. It seems like you get a BSTR (wchar_t*).

BSTR y = ...GetInput...
SysFreeString(y)

Ion Filipski
1c.bmp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top