[Bug c++/66139] destructor not called for members of partially constructed anonymous struct/array

tomaszkam at gmail dot com gcc-bugzilla@gcc.gnu.org
Tue Apr 18 20:36:00 GMT 2017


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66139

--- Comment #2 from Tomasz Kamiński <tomaszkam at gmail dot com> ---
Example of some real life safe-code (now raw allocations, STL container used),
that is leaking:
#include <string>
#include <vector>
#include <iostream>

using namespace std;


struct Test {
    string a;
    string b;
};

Test make_test() {
    vector<string> strings {"123456789abcdef0"};
    return Test {
        strings.at(0),
        strings.at(2) // throws; the destructor for a copy of strings.at(0) is
not called and memory leakds
    };
}

int main() {
    try {
        Test test = make_test();
    } catch (exception &e) {
        cerr << e.what() << endl;
    }
}

Live code proving leaks: https://wandbox.org/permlink/GjjxFw9jAE9bfvyS


More information about the Gcc-bugs mailing list