How to free an icu::Transliterator?

208 views Asked by At

When I do

UErrorCode status = U_ZERO_ERROR;
icu::Transliterator* myTrans = Transliterator::createInstance("Latin-Greek", UTRANS_FORWARD, status);
myTrans->transliterate(...);

and have no further use for myTrans, AddressSanitizer tells me it's leaking memory. I don't see methods like close or free in https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/classicu_1_1Transliterator.html. There is unregister but it will mean I can't create the same transliterator again, right?

1

There are 1 answers

0
Alexey Romanov On

A workaround which works in my particular case: declare

class AutoDeletedTransliterator {
public:
    AutoDeletedTransliterator(const icu::UnicodeString& id, UErrorCode& status)
        : transliterator_(icu::Transliterator::createInstance(
              id, UTRANS_FORWARD, status)) {}
    ~AutoDeletedTransliterator() {
        icu::Transliterator::unregister(transliterator_->getID());
    }
    void transliterate(icu::UnicodeString& string) {
        transliterator_->transliterate(string);
    }

private:
    icu::Transliterator* transliterator_;
};

and use it in a static variable, so the destructor runs at the end of the program.