Re: C++/CLI Unions and Garbage collection

Tech-Archive recommends: Fix windows errors by optimizing your registry



minorguy wrote:
> Then I use these structures from C# and write the following:
>
> MyUnion mu = new MyUnion();
> mu.one = new One();
> mu.two = new Two(); // is the instance of class One now available
> for GC?
> mu.two.a = 42;
>
> Has the memory allocated for One now been made available for garbage
> collection?

Yes.

> Obviousy it would in the typical case when One^ and Two^ don't
> overlap. But what if they do? Does the garbage collector still track
> this?
> ------------------------------------
>
> 2.) For my second question: is it known what the size of a reference
> handle is?
> For example, if I were to instead write MyUnion as:
>
> [StructLayout(LayoutKind::Explicit)]
> public ref struct MyUnion
> {
> [FieldOffset(0)] One^ one;
> [FieldOffset(4)] Two^ two; // <--- note different field
> offset };
>
> Am I allowed to depend on One^ taking up four bytes?

No. The size of a reference is (I believe) undefined. It's certainly not 4
bytes on a 64-bit CLR.

-cd


.