C28651

warning C28651: Static initializer causes copy on write pages due to member function pointers

Static initializers of global or static const variables can often be fully evaluated at compile time, thus generated in RDATA. However if any initializer is a pointer-to-member function where it is a non-static function, the entire initializer may be placed in copy-on-write pages, which has a performance cost.

For binaries that require fast loading and minimizing copy on write pages, consider making sure all function pointers in the static initializer are not pointer-to-member functions. If a pointer-to-member function is required, write a simple static member function that wraps a call to the actual member function.

Example

The following code example generates this error.

void Func()
{
    WCHAR*pszBuf=newWCHAR[MAX_PATH];
    DPA_InsertPtr(_hdpa, DA_LAST, pszBuf);
}

void CleanupDPA()
{
    int count = DPA_GetCount(_hdpa);
    for (int i = 0; i < count; i++)
{
    delete [] (LPWSTR)DPA_GetPtr(_hdpa, i);
}
}  

The following code example avoids this error.

class MyClass
{
    ...
    bool memberFunc();
    static bool memberFuncWrap(MyClass *thisPtr)
        { return thisPtr->memberFunc(); }
    ...
};
const StructType MyStruct[] = {
    ...
    &MyClass::memberFuncWrap,
    ...
};