Report in comment: https://developer.blender.org/T40546#16
Ideally this would have some nicer API function to avoid reallocating the shape key array each time, but this fixes the bug using existing functions.
Otherwise I think the fix is fine for master.