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. SafeHandle.SetOwnership(false)

SafeHandle.SetOwnership(false)

Scheduled Pinned Locked Moved C#
helpcomtutorialquestion
3 Posts 1 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.
  • H Offline
    H Offline
    HosamAly
    wrote on last edited by
    #1

    I have a SafeHandle returned from an external function. I want to set the "ownsHandle" property to false, but the problem is that I'm only able to do this in the constructor. Is there a way to do it? Here is an example:

    class MyHandle : SafeHandleZeroOrMinusOneIsInvalid {
    MyHandle() : base(true) {}

    \[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)\]
    protected override bool ReleaseHandle() {
        externalReleaseHandle(handle);
    }
    
    \[DllImport("MyLib.dll")\] extern void externalReleaseHandle(IntPtr handle);
    

    }

    class MyClass {
    public static void main(string[] args) {
    MyHandle h1 = externalCreateHandle();
    MyHandle h2 = externalGetPointer(h1);
    }

    \[DllImport("MyLib.dll")\] extern void externalCreateHandle();
    \[DllImport("MyLib.dll")\] extern void externalGetPointer(MyHandle handle);
    

    }

    The problem is that I want h1 to be released, but not h2. I thought about using GC.SuppressFinalize(h2), but still calling Dispose() would cause the same problem. Is there a way to do this?

    Many thanks to all helpful guys here in CP! I really appreciate your help. :rose: My LinkedIn Profile

    H 1 Reply Last reply
    0
    • H HosamAly

      I have a SafeHandle returned from an external function. I want to set the "ownsHandle" property to false, but the problem is that I'm only able to do this in the constructor. Is there a way to do it? Here is an example:

      class MyHandle : SafeHandleZeroOrMinusOneIsInvalid {
      MyHandle() : base(true) {}

      \[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)\]
      protected override bool ReleaseHandle() {
          externalReleaseHandle(handle);
      }
      
      \[DllImport("MyLib.dll")\] extern void externalReleaseHandle(IntPtr handle);
      

      }

      class MyClass {
      public static void main(string[] args) {
      MyHandle h1 = externalCreateHandle();
      MyHandle h2 = externalGetPointer(h1);
      }

      \[DllImport("MyLib.dll")\] extern void externalCreateHandle();
      \[DllImport("MyLib.dll")\] extern void externalGetPointer(MyHandle handle);
      

      }

      The problem is that I want h1 to be released, but not h2. I thought about using GC.SuppressFinalize(h2), but still calling Dispose() would cause the same problem. Is there a way to do this?

      Many thanks to all helpful guys here in CP! I really appreciate your help. :rose: My LinkedIn Profile

      H Offline
      H Offline
      HosamAly
      wrote on last edited by
      #2

      Would h2.DangerousAddRef() (without DangerousRelease()) suffice to achieve my objective?

      Many thanks to all helpful guys here in CP! I really appreciate your help. :rose: Know me better

      H 1 Reply Last reply
      0
      • H HosamAly

        Would h2.DangerousAddRef() (without DangerousRelease()) suffice to achieve my objective?

        Many thanks to all helpful guys here in CP! I really appreciate your help. :rose: Know me better

        H Offline
        H Offline
        HosamAly
        wrote on last edited by
        #3

        Sorry, I noticed that this would result in a memory leak for the SafeHandle itself.

        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