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#
  4. How to delete memory allocated by COM?

How to delete memory allocated by COM?

Scheduled Pinned Locked Moved C#
questioncsharpc++comperformance
4 Posts 2 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.
  • R Offline
    R Offline
    RYU
    wrote on last edited by
    #1

    Hi, I have a COM object that actually allocate a memory and return a value to the calling function. When I call this COM object in C++, I can just delete the pointer by using delete keyword. But how can I do this in C#? How to free the memory allocated by the COM object? eg. In my COM class:

    HRESULT __stdcall CTestCOM::getValues(int **arrRetVal)
    {
        int *pTmp = new int[100];
        ...
        ...
        ...
        (*arrRetVal) = pTmp;
        
        return S_OK;
    }
    

    Thanks :)

    R T 2 Replies Last reply
    0
    • R RYU

      Hi, I have a COM object that actually allocate a memory and return a value to the calling function. When I call this COM object in C++, I can just delete the pointer by using delete keyword. But how can I do this in C#? How to free the memory allocated by the COM object? eg. In my COM class:

      HRESULT __stdcall CTestCOM::getValues(int **arrRetVal)
      {
          int *pTmp = new int[100];
          ...
          ...
          ...
          (*arrRetVal) = pTmp;
          
          return S_OK;
      }
      

      Thanks :)

      R Offline
      R Offline
      RYU
      wrote on last edited by
      #2

      Btw, I tried to use both Marshal.FreeCoTaskMem() and Marshal.FreeHGlobal() but it crashes with error:

      Invalid Address specified to RtlFreeHeap( 00210000, 055AB388 )

      Cheers :)

      1 Reply Last reply
      0
      • R RYU

        Hi, I have a COM object that actually allocate a memory and return a value to the calling function. When I call this COM object in C++, I can just delete the pointer by using delete keyword. But how can I do this in C#? How to free the memory allocated by the COM object? eg. In my COM class:

        HRESULT __stdcall CTestCOM::getValues(int **arrRetVal)
        {
            int *pTmp = new int[100];
            ...
            ...
            ...
            (*arrRetVal) = pTmp;
            
            return S_OK;
        }
        

        Thanks :)

        T Offline
        T Offline
        Tony Richards
        wrote on last edited by
        #3

        Why not add a method to the COM class that deletes a pointer that it is provided in a parameter? (Assuming here that .NET doesn't do something nasty to the pointer that makes this impossible/wrong, I'm no expert on COM Interop...)

        R 1 Reply Last reply
        0
        • T Tony Richards

          Why not add a method to the COM class that deletes a pointer that it is provided in a parameter? (Assuming here that .NET doesn't do something nasty to the pointer that makes this impossible/wrong, I'm no expert on COM Interop...)

          R Offline
          R Offline
          RYU
          wrote on last edited by
          #4

          Hi Trichards57, Every good idea. I can pass in the pointer it returns to C# and pass it back to the COM object to delete it. I have to test it first though. Thanks for your help :) Cheers :)

          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