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

--- Comment #1 from jack <jack.cui2 at foxmail dot com> ---
#include <iostream>

using namespace std;

class Block
{
public:
    Block(int n) : data{new char[n]}, size{n}
    {
        cout << "Block ctor\n";
    }

    ~Block()
    {
        cout << "Block dtor\n";
        delete[] data;
    }

private:
    char* data;
    int size;
};

struct Cargo
{
    Block const& block;
};

int main()
{
    {
        Cargo* c = new Cargo{{4000}};
        cout << "main\n";
        delete c;
    }
    return 0;
}

Compile and run:
g++ -Wall -Wextra -fno-strict-aliasing -fwrapv -g -std=c++17

Output:
Block ctor
main

---------------------------
Issue: Block not destructed.

however, below forms are fine
{
    Cargo{{4000}};
    cout << "main";
}

{
    Cargo* c = new Cargo{Block{4000}};
    cout << "main";
    delete c;
}

They all output:

Block ctor
Block dtor
main

Reply via email to