public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/101933] New: Unloaded dll with global std::mutex causes exe to crash on exit #38
@ 2021-08-16 10:23 mailnew4ster at gmail dot com
  2021-08-16 21:20 ` [Bug target/101933] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: mailnew4ster at gmail dot com @ 2021-08-16 10:23 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 101933
           Summary: Unloaded dll with global std::mutex causes exe to
                    crash on exit #38
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mailnew4ster at gmail dot com
  Target Milestone: ---

Reproduction on Windows:

Create and compile dll.cc as following:

```
#include <mutex>
std::mutex x;
```

g++.exe dll.cc -shared -o test.dll

Create and compile exe.cc as following:

```
#include <windows.h>
int main()
{
    auto x = LoadLibrary("test.dll");
    MessageBox(0,0,0,0);
    FreeLibrary(x);
    MessageBox(0,0,0,0);
    return 0;
}
```

g++.exe exe.cc -o test.exe

Run test.exe and observe a crash after seeing both message boxes.
The crash happens in msvcrt.doexit, which tries to call a cleanup function
inside the dll which was already freed.

I'm using TDM-GCC, and I also reported the issue there:
https://github.com/jmeubank/tdm-gcc/issues/38

I'm not sure whether it's specific to TDM-GCC, it applies some patches on GCC,
but they are very small and I think they probably don't affect this issue.

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

* [Bug target/101933] Unloaded dll with global std::mutex causes exe to crash on exit #38
  2021-08-16 10:23 [Bug c++/101933] New: Unloaded dll with global std::mutex causes exe to crash on exit #38 mailnew4ster at gmail dot com
@ 2021-08-16 21:20 ` pinskia at gcc dot gnu.org
  2021-09-03 19:58 ` mailnew4ster at gmail dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-16 21:20 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I suspect it is because __cxa_exit is not used such that deconstructors are not
called at unload of the dll time.

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

* [Bug target/101933] Unloaded dll with global std::mutex causes exe to crash on exit #38
  2021-08-16 10:23 [Bug c++/101933] New: Unloaded dll with global std::mutex causes exe to crash on exit #38 mailnew4ster at gmail dot com
  2021-08-16 21:20 ` [Bug target/101933] " pinskia at gcc dot gnu.org
@ 2021-09-03 19:58 ` mailnew4ster at gmail dot com
  2021-09-03 20:16 ` mailnew4ster at gmail dot com
  2021-09-04  8:56 ` [Bug target/101933] Unloaded dll with global std::mutex causes exe to crash on exit mailnew4ster at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: mailnew4ster at gmail dot com @ 2021-09-03 19:58 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Paul Jackson <mailnew4ster at gmail dot com> ---
I tried to work around the problem, and it's even worse than I expected. You
can replace dll.cc with the following and observe the same crash.

That means that even if I want to do manual memory management with raw
pointers, I can't. Please, consider fixing at least this.

```
#include <vector>

struct {
    std::vector<int> v;
} *test;

void x()
{
    if (test) {
        test->v.push_back(2);
    }
}
```

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

* [Bug target/101933] Unloaded dll with global std::mutex causes exe to crash on exit #38
  2021-08-16 10:23 [Bug c++/101933] New: Unloaded dll with global std::mutex causes exe to crash on exit #38 mailnew4ster at gmail dot com
  2021-08-16 21:20 ` [Bug target/101933] " pinskia at gcc dot gnu.org
  2021-09-03 19:58 ` mailnew4ster at gmail dot com
@ 2021-09-03 20:16 ` mailnew4ster at gmail dot com
  2021-09-04  8:56 ` [Bug target/101933] Unloaded dll with global std::mutex causes exe to crash on exit mailnew4ster at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: mailnew4ster at gmail dot com @ 2021-09-03 20:16 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Paul Jackson <mailnew4ster at gmail dot com> ---
Maybe I'm missing something, but even this dll crashes for me now. So it seems
like it has nothing to do with global variables or static storage. That sucks,
it means that I just can't use C++ in dlls. Or is it? How come nobody noticed
it before?

I hope that I'm missing something here.

```
#include <vector>

void x()
{
    std::vector<int> v;
    v.push_back(2);
}
```

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

* [Bug target/101933] Unloaded dll with global std::mutex causes exe to crash on exit
  2021-08-16 10:23 [Bug c++/101933] New: Unloaded dll with global std::mutex causes exe to crash on exit #38 mailnew4ster at gmail dot com
                   ` (2 preceding siblings ...)
  2021-09-03 20:16 ` mailnew4ster at gmail dot com
@ 2021-09-04  8:56 ` mailnew4ster at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: mailnew4ster at gmail dot com @ 2021-09-04  8:56 UTC (permalink / raw)
  To: gcc-bugs

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

Paul Jackson <mailnew4ster at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #4 from Paul Jackson <mailnew4ster at gmail dot com> ---
I debugged it a bit more, and I found out that:
1. It's happening when exceptions are involved.
2. It's actually a bug of TDM-GCC.

For details, please see my second comment in the GitHub issue:
https://github.com/jmeubank/tdm-gcc/issues/38#issuecomment-912876481

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

end of thread, other threads:[~2021-09-04  8:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-16 10:23 [Bug c++/101933] New: Unloaded dll with global std::mutex causes exe to crash on exit #38 mailnew4ster at gmail dot com
2021-08-16 21:20 ` [Bug target/101933] " pinskia at gcc dot gnu.org
2021-09-03 19:58 ` mailnew4ster at gmail dot com
2021-09-03 20:16 ` mailnew4ster at gmail dot com
2021-09-04  8:56 ` [Bug target/101933] Unloaded dll with global std::mutex causes exe to crash on exit mailnew4ster at gmail dot com

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).