Reputation: 31251
I was reading about memory leaks in managed code and wondered if it was possible to create this in C# unsafe code?
unsafe
{
while(true) new int;
}
I wasn't sure if this would be caught by the GC if this was running as unsafe code?
Thanks
Upvotes: 6
Views: 3687
Reputation: 1061
IMO unsafe only permits to use pointer types and proform C++ style pointer opeartion on memory. But to tell garbage collector not touch my code use fixed statement.
C# supports direct memory manipulation via pointers within blocks of code marked unsafe and compiled with the /unsafe compiler option.
The fixed statement is used to tell the garbage collector to not touch that code that was rounded with fixed statement
unsafe
{
fixed (int* a = &b) // tells garbage collector not touch
{
*a = 9;
}
}
Upvotes: 8
Reputation: 888157
The unsafe
keyword just allows you to use unsafe code (pointers).
It doesn't change the semantics of ordinary code at all.
Upvotes: 11