backported: Fix error "delete called on pointer returned from a mismatched allocation function" See merge request !543