This funciton is modifying cstr (via fptr) so you have to take a strdup copy here and the use MCStringCreateWithCStringAndRelease() at the end (otherwise you are changing the 'in' stringref which isn't allowed). (The MCStringCreateWithCStringAndRelease() form takes ownership of the char_t * buffer you pass to it, meaning you don't have to worry about freeing it - I've just added it to the upstream refactor-syntax_unicode branch).