BUG: Fix memory leak in ndarray construction from strings (gh-2927)

This patch fixes the memory leak in issue gh-2927. The issue appears in both 1.7 and master, so the fix should be backported.