If there's a destructor, use its action instead of free(ptr) (for C)/delete ptr (for C++). Fixes #2108