Patch 1 is an error path fix and depends on Jakob's previous fix for this function. Patch 2 fixes the code that decides whether a buffer object move to another memory type is necessary