Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Marshal.DestroyStructure considered Pure?

Marshal.DestroyStructure is marked with the Pure attribute in the .NET Framework but I'm unsure as to why when it clearly has an effect on the context calling it.

The state is modified (the pointer is freed) even if it doesn't directly modify the pointer instance itself.

Implied in the question is: Can a developer, in good faith, mark something as Pure even if she knows it modifies the context's state indirectly?

like image 479
Rushyo Avatar asked Oct 24 '12 12:10

Rushyo


1 Answers

It's Pure in the sense that it has no effect on visible managed state, which means, for the purpose of code contracts, calling the method could not violate class invariants.

Granted, it's slightly misleading to think of the method as Pure, as it does have side effects, even if those side effects are not visible. They are observable (if you try to use the pointer after freeing it, you'll cause a failure), but not exactly visible (you can't tell without trying to use the pointer that something is wrong.) I'm not sure what the motivation was for marking the method Pure, since I can't see why it would have ever been used in a Contracts block, but I'm guessing there must have been some reason somewhere deep in the BCL contracts that required it.

like image 74
Dan Bryant Avatar answered Sep 22 '22 08:09

Dan Bryant