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. How to release IDispatch memory.

How to release IDispatch memory.

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformancehelptutorialannouncement
7 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.
  • S Offline
    S Offline
    Sampath579
    wrote on last edited by
    #1

    In my application, i am using a ocx. I created an object to one of the interface in the ocx and called a function on that interface from my application. The return type is LPDispatch data. once i call that function, the memory of my exe is increasing since some memory is creating in that function may be. Now my question is how to release that memory once my work is done with LPDispatch data. i called release() but it dint help me. Can some one help me in deleting the memory in my application created by that function in ocx.?

    J L 2 Replies Last reply
    0
    • S Sampath579

      In my application, i am using a ocx. I created an object to one of the interface in the ocx and called a function on that interface from my application. The return type is LPDispatch data. once i call that function, the memory of my exe is increasing since some memory is creating in that function may be. Now my question is how to release that memory once my work is done with LPDispatch data. i called release() but it dint help me. Can some one help me in deleting the memory in my application created by that function in ocx.?

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      You should show us the basic code (how you get the interface pointers). In general, you have to call Release() for each kind of interface object when not used anymore. This applies to all pointers retrieved by calling for example CoCreateInstance and QueryInterface. If a called function is allocating memory internally and returning that to the caller it should be documented. The documentation should then include how to free the memory (e.g. with CoTaskMemFree).

      S 1 Reply Last reply
      0
      • S Sampath579

        In my application, i am using a ocx. I created an object to one of the interface in the ocx and called a function on that interface from my application. The return type is LPDispatch data. once i call that function, the memory of my exe is increasing since some memory is creating in that function may be. Now my question is how to release that memory once my work is done with LPDispatch data. i called release() but it dint help me. Can some one help me in deleting the memory in my application created by that function in ocx.?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Member 10253026 wrote:

        Can some one help me in deleting the memory in my application created by that function in ocx.?

        If your windows application allocates 1GB of RAM... then properly releases it... Yes; the operating system is very lazy in returning that memory back to the system. This is by design. You should not do anything and allow the OS to manage memory. The OS is very good at this. In theory you should be able to do:

        SetProcessWorkingSetSize(-1,-1);

        or

        EmptyWorkingSet(YourPID);

        This will force the OS to release all unused pages assigned to your application. But trust me... this is not necessary and will most likely do more harm than good. Best Wishes, -David Delaune

        1 Reply Last reply
        0
        • J Jochen Arndt

          You should show us the basic code (how you get the interface pointers). In general, you have to call Release() for each kind of interface object when not used anymore. This applies to all pointers retrieved by calling for example CoCreateInstance and QueryInterface. If a called function is allocating memory internally and returning that to the caller it should be documented. The documentation should then include how to free the memory (e.g. with CoTaskMemFree).

          S Offline
          S Offline
          Sampath579
          wrote on last edited by
          #4

          Thanks. Please find the code

          VARIANT Attributessets;
          VariantInit(&Attributessets);

          m_OcxObj->GetAttributes(ID, &Attributessets);

          // here we got the data in variant as LPDispatch....here memory increases

          //even if we do Attributessets.pdispval->Release(), memory is not released

          Attributessets.pdispVal->Release() //it dint release memory.

          J 1 Reply Last reply
          0
          • S Sampath579

            Thanks. Please find the code

            VARIANT Attributessets;
            VariantInit(&Attributessets);

            m_OcxObj->GetAttributes(ID, &Attributessets);

            // here we got the data in variant as LPDispatch....here memory increases

            //even if we do Attributessets.pdispval->Release(), memory is not released

            Attributessets.pdispVal->Release() //it dint release memory.

            J Offline
            J Offline
            Jochen Arndt
            wrote on last edited by
            #5

            How did you know that the memory is not released and how do you determine the memory used by your application? You have to know how the memory is managed which requires knowing the used allocation method. Most memory is allocated in the default per-process heap. If this heap is used up, it is resized by requesting more memory from the operating system. But it will not shrink (at least not immediately) when freeing memory (which occurs when the reference count reaches zero upon releasing). It is finally returned to the operating system when the process terminates. As a result, you might see increasing process memory usage while the process is running. There is a rather old article that explains the heap managment for Windows: Heap: Pleasures and Pains[^]. In your case of a VARIANT set by a OCX function you have no control over the memory management and even did not know which allocation method is used. If an object must be released depends on the data stored in the VARIANT and should be part of the documentation. In your case of a pdispVal, the OCX control might for example increase the reference count for the dispatch when calling GetAttributes(). Then it will be released finally when releasing the OCX control or you have to call Release() which should be again mentioned in the documentation. It depends also on what you are doing with the interface. If you are calling dispatch functions that increase the reference count, you have to call Release() the same times.

            S 1 Reply Last reply
            0
            • J Jochen Arndt

              How did you know that the memory is not released and how do you determine the memory used by your application? You have to know how the memory is managed which requires knowing the used allocation method. Most memory is allocated in the default per-process heap. If this heap is used up, it is resized by requesting more memory from the operating system. But it will not shrink (at least not immediately) when freeing memory (which occurs when the reference count reaches zero upon releasing). It is finally returned to the operating system when the process terminates. As a result, you might see increasing process memory usage while the process is running. There is a rather old article that explains the heap managment for Windows: Heap: Pleasures and Pains[^]. In your case of a VARIANT set by a OCX function you have no control over the memory management and even did not know which allocation method is used. If an object must be released depends on the data stored in the VARIANT and should be part of the documentation. In your case of a pdispVal, the OCX control might for example increase the reference count for the dispatch when calling GetAttributes(). Then it will be released finally when releasing the OCX control or you have to call Release() which should be again mentioned in the documentation. It depends also on what you are doing with the interface. If you are calling dispatch functions that increase the reference count, you have to call Release() the same times.

              S Offline
              S Offline
              Sampath579
              wrote on last edited by
              #6

              Thanks for the information provided. For the question: How did you know that the memory is not released and how do you determine the memory used by your application? we call this Getattributes() method in a for loop. for every iteration, it takes around 8 KB memory and finally at the end of this loop process memory shoots up by 50 MB,which is unwanted for us. we did call Release()method on the pDispval once. but did not work.. i will once again explain the worflow and my questions to give more clarification.. First we get the attributes from the OCX interface using GetAttributes() and we get variant with Pdispvalue.... Does OCX increase the refernce count of Pdispvalue to one here ? Then we attach this pdispval to the OledispatchdriverObject using AttachDispatch()...Does the refrence count go to two ? OCX gets destroyed only when main application ends but increase in the memory effects our application. Do i need to call Release() twice on the pDispval to make it clear ? Does memory gets freed at the end of second Release() call or is it the OCX destruction that clears it.

              J 1 Reply Last reply
              0
              • S Sampath579

                Thanks for the information provided. For the question: How did you know that the memory is not released and how do you determine the memory used by your application? we call this Getattributes() method in a for loop. for every iteration, it takes around 8 KB memory and finally at the end of this loop process memory shoots up by 50 MB,which is unwanted for us. we did call Release()method on the pDispval once. but did not work.. i will once again explain the worflow and my questions to give more clarification.. First we get the attributes from the OCX interface using GetAttributes() and we get variant with Pdispvalue.... Does OCX increase the refernce count of Pdispvalue to one here ? Then we attach this pdispval to the OledispatchdriverObject using AttachDispatch()...Does the refrence count go to two ? OCX gets destroyed only when main application ends but increase in the memory effects our application. Do i need to call Release() twice on the pDispval to make it clear ? Does memory gets freed at the end of second Release() call or is it the OCX destruction that clears it.

                J Offline
                J Offline
                Jochen Arndt
                wrote on last edited by
                #7

                Member 10253026 wrote:

                First we get the attributes from the OCX interface using GetAttributes() and we get variant with Pdispvalue.... Does OCX increase the refernce count of Pdispvalue to one here ?

                This should be part of the OCX documentation. I guess that the reference count is incremented.

                Quote:

                Then we attach this pdispval to the OledispatchdriverObject using AttachDispatch()...Does the reference count go to two ?

                AttachDispatch() will increment the reference count. So you have to call Release() or set bAutoRelease. But when using AttachDispatch() your OleDispatchDriver object will be the owner of the interface. So you should not use the pDispVal member of the VARIANT afterwards anymore (set it to NULL to avoid using it). Memory is only freed when the reference count becomes zero when calling Release(). I suggest to check the return value when calling Release(). Then you will know when memory is freed. To check the actual reference count without releasing, just call AddRef() and Release().

                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