ReliabilityContractAttribute
-
Hello, I am trying to understand the CER feature, but I am not very confident that I understand it well. I have read a number of articles about it, but I still need to check whether I understand it correctly. I hope you could help me. First, I apologize for this long post. I thought it is necessary to clarify my question. I'm thankful for your patience. I am implementing a
SafeHandle
, and the release code looks like this:class MySafeHandle : SafeHandle {
protected override bool ReleaseHandle() {
MyClass.freeHandle(handle);
return !MyClass.CheckErrors(false);
}
}
class MyClass {
internal static extern void freeHandle(MySafeHandle handle);
internal static extern bool checkExternalErrors();internal static bool CheckErrors(bool throwExceptionOnError) { if (!checkExternalErrors()) return false; if (throwExceptionOnError) throw new Exception(); return true; }
}
I'm not sure what to label
freeHandle
,checkExternalErrors
andCheckErrors
.freeHandle
wouldn't tell whether there is an error; I have to callCheckErrors
for that. But it will never throw an exception either, so I'm thinking it should be labeled[Cer.Success]
. Meanwhile,CheckErrors
may throw an exception, depending on thethrowExceptionOnError
parameter. So I think this should be labeled[Cer.MayFail]
. But I don't really know... wouldn't that violate the CER onReleaseHandle
? Also what should I labelcheckExternalErrors
? Does it not need a label? Or does it still need[ReliabilityContractAttribute(Consistency.WillNotCorruptState)]
? I really feel confused! Thanks a lot for your patience! Even if you don't answer, I'm grateful that you read this post to the end. :)