In Microsoft's example for how to use the PixelShader they use a singleton. I've seen the same pattern in other places, and here they say
The pixel shader is stored in a private static field _pixelShader. This field is static, because one instance of the compiled shader code is enough for the whole class.
We've seen several memory leak issues when using this pattern. The PixelShader is involved is event handling that don't always get cleared correctly. We had to freeze them, and saw some improvement. We had to manually do some detachments
// Attach/detach effect as UI element is loaded/unloaded. This avoids
// a memory leak in the shader code as described here:
element.Loaded += (obj, args) =>
{
effect.PixelShader = ms_shader;
element.Effect = effect;
};
element.Unloaded += (obj, args) =>
{
effect.PixelShader = null;
element.Effect = null;
};
And even now under stress there are still memory leaks in that area. Does anyone know if the PixelShader uses heavy resources worth the trouble with using a singleton?