Simon,

> MC++ won't allow you to declare a managed pointer
> as a member of an unmanaged class, but requires you
> to use gcroot<> instead.
>
> However declaring a managed pointer as a local
> variable in a function or as an argument to a function in an
> unmanaged class does NOT produce a compilation error,
> and there's no word in the docs about using gcroot
> here. I'm assuming you are still supposed to use
> gcroot<> in all managed pointer declarations - can
> anyone confirm if that is correct?

Valery already pointed out the reason why it is not needed. But think of it
this way, and it will become very clear why not:
When you declare it as an argument or local variable of a function, your
needs to be compiled as managed code (that is, under /clr and in a #pragma
managed section), which should be the default. So, that's just managed IL
code, so the runtime itself can track the lifetime of the object reference
defined by the variable/argument... no extra suport from the compiler is
really needed to ensure this works.

However, when you want to hold on to an object reference as a member of a
__nogc class, that class itself could be allocated where the managed runtime
has no control, such as in the unmanaged heap. So, extraq support, in the
form of GCHandle (or gcroot, which wraps it) is needed to tell the runtime
that you still hold a reference to the object.
--
Tomas Restrepo
[EMAIL PROTECTED]

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to