troubleshooting Question

Trying to wrap IntPtr in an unmanaged nested class using gcroot<IntPtr*> fails miserably

Avatar of Xtreem
XtreemFlag for United Kingdom of Great Britain and Northern Ireland asked on
Visual C++.NET
11 Comments1 Solution1464 ViewsLast Modified:
I have created this nested class in my managed C++ class:

    __nogc class IntPtrWrapper
    {
    public:
        IntPtrWrapper(const int size) { ip = new IntPtr(0); }
        ~IntPtrWrapper(void) { }

        gcroot<IntPtr*> ip;
    };

which fails with this error:

    error C2716: 'operator new' allocates value types on the C++ heap; use '__nogc new System::IntPtr'

If I then do as it says and replace 'ip = new IntPtr(0)' with 'ip = __nogc new IntPtr(0)' giving me:

    __nogc class IntPtrWrapper
    {
    public:
        IntPtrWrapper(const int size) { ip = __nogc new IntPtr(0); }
        ~IntPtrWrapper(void) { }

        gcroot<IntPtr*> ip;
    };

I get this error:

    gcroot.h(98): error C2664: 'System::Runtime::InteropServices::GCHandle::set_Target' : cannot convert parameter 1 from 'System::IntPtr __gc *' to 'System::Object __gc *'

If I try to referene ip, for example by adding the following line to the constructor:

    *ip = Marshal::AllocHGlobal(size);

I get this error:

    gcroot.h(111): error C2440: 'static_cast' : cannot convert from 'System::Object __gc *' to 'System::IntPtr __gc *'


I've followed the instructions given on MSDN which uses a String for the example (as opposed to IntPtr):
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmxspec/html/vcManagedExtensionsSpec_16_3.asp

Even if I change my declaration of ip to gcroot<IntPtr> and change the references to it accordingly so I have the following code:

    __nogc class IntPtrWrapper
    {
    public:
        IntPtrWrapper(const int size)
        {
            ip = __nogc new IntPtr(0);
            ip = Marshal::AllocHGlobal(size);
        }
        ~IntPtrWrapper(void) { }

        gcroot<IntPtr> ip;
    };

I still get error C2664, same as above:

    gcroot.h(98): error C2664: 'System::Runtime::InteropServices::GCHandle::set_Target' : cannot convert parameter 1 from 'System::IntPtr' to 'System::Object __gc *'


I've tried all sorts, even using __box, but to no avail. Can anyone help me or is IntPtr just not supposed to be wrapped by gcroot? All I'm trying to do essentially is wrap IntPtr so I can manage the freeing of memory (through 'Marshal::FreeHGlobal(*ip)' in the destructor) automatically.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 11 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 11 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros