public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* g++ problem with order of evaluation of arguments of delete.
@ 2023-05-04  9:45 Georg-Johann Lay
  2023-05-04  9:58 ` LIU Hao
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Georg-Johann Lay @ 2023-05-04  9:45 UTC (permalink / raw)
  To: gcc-help

Given the following C++ code:

struct Lexer;

struct Token
{
     Lexer* const lexer_;
     Token (Lexer *l) : lexer_(l) {}
     ~Token() = default;

     Token() = delete;
     Token (const Token&) = delete;
     Token (Token&&) = delete;
     void operator= (const Token&) = delete;
     void operator= (Token&&) = delete;
};

struct Lexer
{
     Token *token_;
     Lexer() = default;
     ~Lexer() { delete token_; }

     Lexer (const Lexer&) = delete;
     Lexer (Lexer&&) = delete;
     void operator= (const Lexer&) = delete;
     void operator= (Lexer&&) = delete;
};

int main()
{
     Lexer *lexer = new Lexer();
     Token *token = new Token (lexer);
     lexer->token_ = token;
     delete token->lexer_;
     // delete lexer; // is OK
}

When I compile this with g++ v11.3 (same with g++ from master from 
2023-04-20) and run

$ g++ main-3.cpp -Os -W -Wall -Wextra -dumpbase "" -save-temps -dp  && 
./a.out

Segmentation fault (core dumped)

The assembly shows that the generated code does two calls to "delete" 
but just one call to "new", so it's clear something is going wrong.

As far as I understand, the "delete token_" in ~Lexer is a sequence 
point, so that dereferencing token in "delete->lexer_" must be sequenced 
before calling ~Token ?

Segmentation fault also occurs with -O0, but goes away when removing the 
"const" in "Lexer* const lexer_;".

My question: Is this a GCC problem, or a problem with the code and 
sequence points?

Johann

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2023-05-10  1:31 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-04  9:45 g++ problem with order of evaluation of arguments of delete Georg-Johann Lay
2023-05-04  9:58 ` LIU Hao
2023-05-04 10:02   ` Jonathan Wakely
2023-05-04 10:00 ` Marc Glisse
2023-05-04 10:06 ` Jonathan Wakely
2023-05-04 10:12   ` Jonathan Wakely
2023-05-04 10:25     ` Jonathan Wakely
2023-05-04 10:54       ` Georg-Johann Lay
2023-05-04 10:38     ` Georg-Johann Lay
2023-05-04 10:44       ` Jonathan Wakely
2023-05-10  1:31     ` LIU Hao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).