cocos / cocos-engine

Cocos simplifies game creation and distribution with Cocos Creator, a free, open-source, cross-platform game engine. Empowering millions of developers to create high-performance, engaging 2D/3D games and instant web entertainment.
https://www.cocos.com/en/creator
Other
8.32k stars 1.95k forks source link

Memory leak in StringPool #10762

Closed minggo closed 2 years ago

minggo commented 2 years ago
inline StringHandle StringPool<ThreadSafe>::doStringToHandle(const char *str) noexcept {
    const auto it = _stringToHandles.find(str);

    if (it == _stringToHandles.end()) {
        size_t const strLength = strlen(str) + 1;
        char *const  strCache  = new char[strLength];  // memory leak: create memory, but do't find anywhere to free the memory
        strcpy(strCache, str);
        StringHandle name(static_cast<StringHandle::IndexType>(_handleToStrings.size()), strCache);
        _handleToStrings.emplace_back(strCache);
        _stringToHandles.emplace(strCache, name);
        return name;
    }
    return it->second;
}

Currently, StringPool is used in FrameGraph. Maybe can use ccstd::string instead?

minggo commented 2 years ago

Oh, the memory is freed in destructor.