Detecting Unmatched Calls to GetPointerTo Accessor Function
- Updated2023-02-21
- 1 minute(s) read
Detecting Unmatched Calls to GetPointerTo Accessor Function
You can specify the maximum number of nested calls to the GetPointerTo accessor function through the last parameter (maxGetPointerNesting) to the DefineThreadSafeScalarVar and DefineThreadSafeArrayVar macros. You should generally pass 0 for this parameter so that the GetPointerTo function reports a run-time error when it detects two consecutive calls to GetPointerTo from the same thread without an intervening call to ReleasePointerTo. For example, the following code generates a run-time error the second time it is executed because it is missing a call to ReleasePointerToCount.
int IncrementCount (void)
{
int *countPtr;
countPtr = GetPointerToCount(); /* run-time error on second execution of this line */
(*countPtr)++;
/* missing call to ReleasePointerToCount here */
return 0;
}
Pass an integer greater than zero as the maxGetPointerNesting parameter if your code makes nested calls to GetPointerTo. For example, the following code sets the maxGetPointerNesting parameter to 1 because it is valid to make one level of nested calls to GetPointerTo.
DefineThreadSafeScalarVar (int, Count, 1);
int Count (void)
{
int *countPtr;
countPtr = GetPointerToCount();
(*countPtr)++;
DoSomethingElse(); /* calls GetPointerToCount */
ReleasePointerToCount ();
return 0;
}
void DoSomethingElse(void)
{
int *countPtr;
countPtr = GetPointerToCount(); /* nested call to GetPointerToCount */
/* do something with countPtr */
ReleasePointerToCount ();
}
If you do not know the maximum nesting level for GetPointerTo, pass TSV_ALLOW_UNLIMITED_NESTING to disable checking for unmatched GetPointerTo calls.
GetPointerTo checks the nesting level only when your code is compiled for debugging.