Skip to content
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

Make sure all singletons are set to null and make env a global ptr #44

Merged
merged 2 commits into from Oct 17, 2017

Conversation

fireice-uk
Copy link
Owner

This change makes sure of two things:

  • Singleton ptr is always null at first
  • We have only single env instance, so we don't duplicate singletons if we create them after the dll is loaded

Not testes at all, but should work. Let me know if there are any problems


static environment& inst()
{
static environment env;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Normally this is the comon way to create a singleton and is free of memory leeks. Is there a reason why you have changed it?

This version is also race condition free and guarantee that there exists only one version of this class.

Copy link
Owner Author

Choose a reason for hiding this comment

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

This is because you are creating a copy of the environment object in DLLs. If you create a singleton after you create a DLL copy you will have a duplicate.

Copy link
Owner Author

Choose a reason for hiding this comment

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

@psychocrypt Can you let me know if you approve or not here?

@psychocrypt
Copy link
Collaborator

I approved this PR but can't test it currently in windows. Do you tested it in windows?

@fireice-uk
Copy link
Owner Author

There is no OS-specific code in that PR - merging.

@fireice-uk fireice-uk merged commit dcd176a into dev Oct 17, 2017
@psychocrypt
Copy link
Collaborator

psychocrypt commented Oct 17, 2017 via email

@fireice-uk
Copy link
Owner Author

What's the difference? There is none from what I can see in the code.

@psychocrypt psychocrypt deleted the fix-uninit-access branch October 17, 2017 19:13
@psychocrypt
Copy link
Collaborator

In the current implementation there is no difference. But the reason why we need to path the singleton class environment to the dll/so is that a singleton under windows is only unique within a shared libarary. This means if the main binary create a singleton the singleton is not visible in the dll. In linux a singleton is which is created in the shared library or in the main binary is unique ofer all shared libraries and the main binary.
There are whys in the microsoft knowlage base collection where some ways are descriped how you can achieve it under windows but none of this worked in my tests.
That's the reason why all all singleton pointer are living in the environment class.

@fireice-uk
Copy link
Owner Author

You mean the difference in symbol visibility between Windows and Linux? If not then please link to the MSDN article.

@psychocrypt
Copy link
Collaborator

You mean the difference in symbol visibility between Windows and Linux?

Yes, for me as linux developer this was absolutely new, that a singleton is not unique because of the difference in the symbol export handling.

Sry I can't point to the sources I worked on this during the refactoring and have not stored it :-(

note to myself: bookmark links

executor* pExecutor;
params* pParams;

printer* pPrinter = nullptr;
Copy link
Collaborator

Choose a reason for hiding this comment

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

@fireice-uk Could you please point me to a reference which describe why it is allowed to initialize member out of the constructor. Is this because of initializer lists?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Owner Author

Choose a reason for hiding this comment

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

That's correct. For C++11 both those styles are equivalent. I chose the second one because it is clearer and nullptr is a constexpr.

if(env == nullptr)
{
if(init == nullptr)
env = new environment;
Copy link
Collaborator

Choose a reason for hiding this comment

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

@fireice-uk Could it be that this is the reason for #51. That environment is handled as POD and the pointer will not be initilized with a nullptr?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

If this can result in a uninitialized type than we need also check all other singletons.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants