Memory leak [modified]
-
Hi Gurus! I have an MFC activeX component which creates a memory leak in the application that uses it. The leak is when exiting program. I have created a small example to show you what happens. In my activeX component I have this method; Code: LONG CThursdayActiveXCtrl::getSomething(VARIANT* pVar) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); // A safe array of float CComSafeArray saVars; // Insert elements into the CComSafeArray saVars.Add(1); //THIS LINE GIVES A LEAK!!! SEE BELOW saVars.Add(32); saVars.Add(43); // A CComVariant to return to the client CComVariant varReturn (saVars.Detach ()); varReturn.vt=VT_R4; return varReturn.Detach (pVar); } I have added my activex control to a dialog based MFC application. I have a button, when that button is pushed this method is called: Code: void Ctest1Dlg::OnBnClickedButton1() { CComVariant varArray; // Get the array from the server test.getSomething(&varArray); // Attach the safe array CComSafeArray saFromVariant; saFromVariant.Attach (varArray.parray); a=saFromVariant.GetAt(0); b=saFromVariant.GetAt(1); c=saFromVariant.GetAt(2); UpdateData(false); } My debugging tool, which is DevPartner gives me the following memory leak: Memory Leak Exiting Program: Address 0x024101A0 (40) allocated by SafeArrayCreate. The call stack gives me this lines of code causing the leak; atlsafe.h 534 atlsafe.h 521 atlsafe.h 373 getSomething 195 (which is the line I have marked in the code above). Can anyone help me out here? This leak is 40 bytes big, and that will become quite a large amount when the method in my real control is invoked approx. 5 times a second!! Thanks for any help! -- modified at 14:12 Thursday 29th June, 2006
-
Hi Gurus! I have an MFC activeX component which creates a memory leak in the application that uses it. The leak is when exiting program. I have created a small example to show you what happens. In my activeX component I have this method; Code: LONG CThursdayActiveXCtrl::getSomething(VARIANT* pVar) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); // A safe array of float CComSafeArray saVars; // Insert elements into the CComSafeArray saVars.Add(1); //THIS LINE GIVES A LEAK!!! SEE BELOW saVars.Add(32); saVars.Add(43); // A CComVariant to return to the client CComVariant varReturn (saVars.Detach ()); varReturn.vt=VT_R4; return varReturn.Detach (pVar); } I have added my activex control to a dialog based MFC application. I have a button, when that button is pushed this method is called: Code: void Ctest1Dlg::OnBnClickedButton1() { CComVariant varArray; // Get the array from the server test.getSomething(&varArray); // Attach the safe array CComSafeArray saFromVariant; saFromVariant.Attach (varArray.parray); a=saFromVariant.GetAt(0); b=saFromVariant.GetAt(1); c=saFromVariant.GetAt(2); UpdateData(false); } My debugging tool, which is DevPartner gives me the following memory leak: Memory Leak Exiting Program: Address 0x024101A0 (40) allocated by SafeArrayCreate. The call stack gives me this lines of code causing the leak; atlsafe.h 534 atlsafe.h 521 atlsafe.h 373 getSomething 195 (which is the line I have marked in the code above). Can anyone help me out here? This leak is 40 bytes big, and that will become quite a large amount when the method in my real control is invoked approx. 5 times a second!! Thanks for any help! -- modified at 14:12 Thursday 29th June, 2006
pierre_ribery wrote:
varReturn.vt=VT_R4;
This is part of your problem. It should be the following:
varReturn.vt = VT_R4 | VT_ARRAY;
Otherwise, when the "smart" classes call ::ClearVariant, it won't be performing the proper cleanup procedure. If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac -
pierre_ribery wrote:
varReturn.vt=VT_R4;
This is part of your problem. It should be the following:
varReturn.vt = VT_R4 | VT_ARRAY;
Otherwise, when the "smart" classes call ::ClearVariant, it won't be performing the proper cleanup procedure. If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week ZacThanks for your reply! You say part of my problem, is there more that could be corrected? And, btw, the leaks are still there after I made the change you suggested! I am really lost of how to fix this! Pierre.
-
Hi Gurus! I have an MFC activeX component which creates a memory leak in the application that uses it. The leak is when exiting program. I have created a small example to show you what happens. In my activeX component I have this method; Code: LONG CThursdayActiveXCtrl::getSomething(VARIANT* pVar) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); // A safe array of float CComSafeArray saVars; // Insert elements into the CComSafeArray saVars.Add(1); //THIS LINE GIVES A LEAK!!! SEE BELOW saVars.Add(32); saVars.Add(43); // A CComVariant to return to the client CComVariant varReturn (saVars.Detach ()); varReturn.vt=VT_R4; return varReturn.Detach (pVar); } I have added my activex control to a dialog based MFC application. I have a button, when that button is pushed this method is called: Code: void Ctest1Dlg::OnBnClickedButton1() { CComVariant varArray; // Get the array from the server test.getSomething(&varArray); // Attach the safe array CComSafeArray saFromVariant; saFromVariant.Attach (varArray.parray); a=saFromVariant.GetAt(0); b=saFromVariant.GetAt(1); c=saFromVariant.GetAt(2); UpdateData(false); } My debugging tool, which is DevPartner gives me the following memory leak: Memory Leak Exiting Program: Address 0x024101A0 (40) allocated by SafeArrayCreate. The call stack gives me this lines of code causing the leak; atlsafe.h 534 atlsafe.h 521 atlsafe.h 373 getSomething 195 (which is the line I have marked in the code above). Can anyone help me out here? This leak is 40 bytes big, and that will become quite a large amount when the method in my real control is invoked approx. 5 times a second!! Thanks for any help! -- modified at 14:12 Thursday 29th June, 2006
I think one of the problems is this assignment:
varReturn.vt = VT_R4;
I suppose this is unneeded, since
varReturn.vt
already contains a value, which isVT_ARRAY | VT_R4
. What happens when you comment this line? Next, the constructor atCComVariant varReturn(saVars.Detach());
creates a copy of passed
SAFEARRAY
object, therefore the source detached object is not deleted and causes memory leaks. I think you should first try this:CComVariant varReturn((LPSAFEARRAY)saVars);
If it works, you can reconsider your
getSomething
function and return aSAFEARRAY
pointer directly, detached fromsaVars
. In the other function, attach this pointer to aCComSafeArray
variable. It will be deleted automatically. I hope it helps. -- modified at 10:22 Thursday 29th June, 2006 -
Thanks for your reply! You say part of my problem, is there more that could be corrected? And, btw, the leaks are still there after I made the change you suggested! I am really lost of how to fix this! Pierre.
I said "part of" because I believe you don't need the Detach/Attach methods as well (just not sure on that ... its been a little while since I dealt with Microsoft's COM wrappers). If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
I think one of the problems is this assignment:
varReturn.vt = VT_R4;
I suppose this is unneeded, since
varReturn.vt
already contains a value, which isVT_ARRAY | VT_R4
. What happens when you comment this line? Next, the constructor atCComVariant varReturn(saVars.Detach());
creates a copy of passed
SAFEARRAY
object, therefore the source detached object is not deleted and causes memory leaks. I think you should first try this:CComVariant varReturn((LPSAFEARRAY)saVars);
If it works, you can reconsider your
getSomething
function and return aSAFEARRAY
pointer directly, detached fromsaVars
. In the other function, attach this pointer to aCComSafeArray
variable. It will be deleted automatically. I hope it helps. -- modified at 10:22 Thursday 29th June, 2006Thanks mate! There is no rating system here right? (well you got it anyway somewhere!)