简体   繁体   中英

C++11 , leaking memory

I have a case which the instrument of Xcode detected

std::vector<UserID_t> functions::getUserIds() const {
    static_assert(sizeof(int64) == sizeof(uint64_t), "size is not matched");
    auto object = AAAAA::BBBB::ValueObject<int64 *>(_hash->getValue((nByte)Key::USER_IDS));
    auto size = object.getSizes();
    std::vector<UserID_t> ret(*size);
    auto pVal = object.getDataCopy();
    for (int index = 0; index < *size; index++) {
        ret[index] = *pVal;
        ++pVal;
    }
    return ret;
}

The tool shows me that the elements in "ret" are not released after return. But I think int64 is a scalar variable, I don't need to release them. Is that right?

I use Xcode 6.3.2

Using auto there with pointers (assuming they are pointers and not some class with an overloaded * operator) is making things confusing. I think the error might be in the fact that you are creating a "copy" in object.getDataCopy() , returning a pointer to it and not deleting it before the return of functions::getUserIds()

The name of the function .getSize s (); implies that it returns an array of sizes?

Or it's just a bad API name.

What's the actual type of size ?

UPDATE: the culprit is found in the comments to be the getDataCopy() which makes a deep copy which is then never deallocated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM