Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Memory leak [modified]

Memory leak [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresc++comsysadminperformance
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pierre_ribery
    wrote on last edited by
    #1

    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

    Z V 2 Replies Last reply
    0
    • P pierre_ribery

      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

      Z Offline
      Z Offline
      Zac Howland
      wrote on last edited by
      #2

      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

      P 1 Reply Last reply
      0
      • Z Zac Howland

        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

        P Offline
        P Offline
        pierre_ribery
        wrote on last edited by
        #3

        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.

        Z 1 Reply Last reply
        0
        • P pierre_ribery

          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

          V Offline
          V Offline
          Viorel
          wrote on last edited by
          #4

          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 is VT_ARRAY | VT_R4. What happens when you comment this line? Next, the constructor at

          CComVariant 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 a SAFEARRAY pointer directly, detached from saVars. In the other function, attach this pointer to a CComSafeArray variable. It will be deleted automatically. I hope it helps. -- modified at 10:22 Thursday 29th June, 2006

          P 1 Reply Last reply
          0
          • P pierre_ribery

            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.

            Z Offline
            Z Offline
            Zac Howland
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • V Viorel

              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 is VT_ARRAY | VT_R4. What happens when you comment this line? Next, the constructor at

              CComVariant 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 a SAFEARRAY pointer directly, detached from saVars. In the other function, attach this pointer to a CComSafeArray variable. It will be deleted automatically. I hope it helps. -- modified at 10:22 Thursday 29th June, 2006

              P Offline
              P Offline
              pierre_ribery
              wrote on last edited by
              #6

              Thanks mate! There is no rating system here right? (well you got it anyway somewhere!)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups