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. Managed C++/CLI
  4. Handles and pointers...

Handles and pointers...

Scheduled Pinned Locked Moved Managed C++/CLI
question
3 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.
  • J Offline
    J Offline
    jarl
    wrote on last edited by
    #1

    Is this; void bar( data_struct_t * structure ) { stuff_handle_ = GCHandle::Alloc( structure ); data_struct_t * tmp = static_cast<data_struct_t*>(stuff_handle_.Target); tmp->data_ = 1; } effectively as safe as, or the same as, this ? void bar( data_struct_t * structure ) { data_struct_t __pin * pinned = structure; pinned->data_ = 1; } -=jarl=-

    J 1 Reply Last reply
    0
    • J jarl

      Is this; void bar( data_struct_t * structure ) { stuff_handle_ = GCHandle::Alloc( structure ); data_struct_t * tmp = static_cast<data_struct_t*>(stuff_handle_.Target); tmp->data_ = 1; } effectively as safe as, or the same as, this ? void bar( data_struct_t * structure ) { data_struct_t __pin * pinned = structure; pinned->data_ = 1; } -=jarl=-

      J Offline
      J Offline
      Jeff J
      wrote on last edited by
      #2

      Not really. GCHandle is more versatile than a pointer pinned via __pin. A pinned pointer only remains pinned while it is in scope, and if not set to zero. For example, when a function that uses pinned pointers returns, any pinned objects are no longer pinned. I suppose one could just say the __pin is used on stack-allocated pointers. GCHandle, on the other hand, allocates on the heap (which is why Free() must be called to release an Alloc()-allocated GCHandle). There are a few options for GCHandles, but a regular one stays allocated between function calls, like any object allocated via malloc() or unmanaged new. Which is safer? Well, that depends on the lifetime requirements you have for the object in question. If the handle needs to remain pinned/valid when it goes out of scope, GCHandle would be the safe bet. Cheers

      J 1 Reply Last reply
      0
      • J Jeff J

        Not really. GCHandle is more versatile than a pointer pinned via __pin. A pinned pointer only remains pinned while it is in scope, and if not set to zero. For example, when a function that uses pinned pointers returns, any pinned objects are no longer pinned. I suppose one could just say the __pin is used on stack-allocated pointers. GCHandle, on the other hand, allocates on the heap (which is why Free() must be called to release an Alloc()-allocated GCHandle). There are a few options for GCHandles, but a regular one stays allocated between function calls, like any object allocated via malloc() or unmanaged new. Which is safer? Well, that depends on the lifetime requirements you have for the object in question. If the handle needs to remain pinned/valid when it goes out of scope, GCHandle would be the safe bet. Cheers

        J Offline
        J Offline
        jarl
        wrote on last edited by
        #3

        Ok, thanks! -=jarl=-

        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