Skip to content

Fix crash due to static variable initialization order #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

rcdailey
Copy link

The static variable equal_string_ordinal_ic is not yet initialized when
invoking boost::filesystem::remove_all(dir) also at static initialization
time.

Real example code that causes the issue (global scope):

/// \hack
/// Forces 1 time shared memory cleanup on filesystem.
static class cleanup_shared_memory
{
public:
   cleanup_shared_memory()
   {
      try
      {
         std::string dir;
         ipcdetail::get_shared_dir_root(dir);
         boost::filesystem::remove_all(dir);
         LOG_INFO << "Removed old shared memory data: " << dir;
      }
      catch (boost::filesystem::filesystem_error const& e)
      {
         LOG_ERROR << "Could not remove old shared memory data: " << e.what();
      }
   }
} do_not_remove;

The static variable `equal_string_ordinal_ic` is not yet initialized when
invoking `boost::filesystem::remove_all(dir)` also at static initialization
time.

Real example code that causes the issue (global scope):

```
/// \hack
/// Forces 1 time shared memory cleanup on filesystem.
static class cleanup_shared_memory
{
public:
   cleanup_shared_memory()
   {
      try
      {
         std::string dir;
         ipcdetail::get_shared_dir_root(dir);
         boost::filesystem::remove_all(dir);
         LOG_INFO << "Removed old shared memory data: " << dir;
      }
      catch (boost::filesystem::filesystem_error const& e)
      {
         LOG_ERROR << "Could not remove old shared memory data: " << e.what();
      }
   }
} do_not_remove;
```

perms make_permissions(const path& p, DWORD attr)
{
static Ptr_equal_string_ordinal_ic equal_string_ordinal_ic =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function-local statics are not thread-safe in C++03.

Copy link
Author

@rcdailey rcdailey Aug 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Statics aren't thread safe period. I expect to see some synchronization happening here if this needs to be thread safe. I don't see how my change affects the threading aspect of the code, since a global isn't thread safe either.

@rcdailey
Copy link
Author

rcdailey commented Aug 8, 2017

Closing in favor of #43 (mine is a duplicate of that and more discussion has happened there).

@rcdailey rcdailey closed this Aug 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants