[Bug c++/105265] temporary object not destructed causing memory leaks
jack.cui2 at foxmail dot com
gcc-bugzilla@gcc.gnu.org
Wed Apr 13 14:15:30 GMT 2022
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
More information about the Gcc-bugs
mailing list