This looks like an overkill. You literally use these 'smart pointers' 2 times. Two of these functions, each used 1 time. Why do you need all of that? Is it too hard to add explicit tuple_ref/unref in these 2 places?