Managing memory correctly is an important part of programming against the user defined function API in C and C++. One central concept of memory management is ownership. In general, if your code owns some memory, that memory is your responsibility, and you must release it appropriately (sometimes with the sqlmr_release function and sometimes with a type-specific sqlmr_*_releaseOwned function, as described in the API reference documentation). For memory that your code does not own, the user defined function framework will release the memory, so your code must not release it or modify it.
Functions in the user defined function API never take ownership of data given to them, but they may transfer ownership to the caller. The reference documentation for the user defined function API describes the rules for memory ownership in detail on a function-by-function basis.