-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,35 +12,30 @@ class params; | |
|
||
struct environment | ||
{ | ||
|
||
static environment& inst() | ||
{ | ||
static environment env; | ||
return env; | ||
} | ||
|
||
environment& operator=(const environment& env) | ||
static inline environment& inst(environment* init = nullptr) | ||
{ | ||
this->pPrinter = env.pPrinter; | ||
this->pglobalStates = env.pglobalStates; | ||
this->pJconfConfig = env.pJconfConfig; | ||
this->pExecutor = env.pExecutor; | ||
this->pParams = env.pParams; | ||
return *this; | ||
} | ||
static environment* env = nullptr; | ||
|
||
if(env == nullptr) | ||
{ | ||
if(init != nullptr) | ||
env = new environment; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
else | ||
env = init; | ||
} | ||
|
||
environment() : pPrinter(nullptr), pglobalStates(nullptr) | ||
{ | ||
return *env; | ||
} | ||
|
||
environment() | ||
{ | ||
} | ||
|
||
printer* pPrinter; | ||
globalStates* pglobalStates; | ||
jconf* pJconfConfig; | ||
executor* pExecutor; | ||
params* pParams; | ||
|
||
printer* pPrinter = nullptr; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
globalStates* pglobalStates = nullptr; | ||
jconf* pJconfConfig = nullptr; | ||
executor* pExecutor = nullptr; | ||
params* pParams = nullptr; | ||
}; | ||
|
||
} // namepsace xmrstak |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?