user978122
user978122

Reputation: 5761

GC and unmanaged code in C# / C++

I have a silly question. If I have an array in C++, that I am using to store a number of references (I'm guessing IntPtrs, haven't gotten that far yet) to various C# objects, how does the GC in C# know not to collect these objects? I mean, the mark and sweep algorithm can't see into the unmanaged code structure, right? So, if the only reference to these C# objects is inside this structure, would I not have to do something extra to tell the GC not to collect them?

Upvotes: 1

Views: 355

Answers (1)

Sten Petrov
Sten Petrov

Reputation: 11040

Yes you would have to take special care to ensure not only your objects are kept alive and safe from GC but that they haven't been moved around by GC.

Take a look at: http://dotnet.dzone.com/news/net-memory-control-use-gchandl

Upvotes: 3

Related Questions