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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion xmrstak/backend/amd/minethd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ __declspec(dllexport)
#endif
std::vector<iBackend*>* xmrstak_start_backend(uint32_t threadOffset, miner_work& pWork, environment& env)
{
environment::inst() = env;
environment::inst(&env);
return amd::minethd::thread_starter(threadOffset, pWork);
}
} // extern "C"
Expand Down
2 changes: 1 addition & 1 deletion xmrstak/backend/nvidia/minethd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ __declspec(dllexport)
#endif
std::vector<iBackend*>* xmrstak_start_backend(uint32_t threadOffset, miner_work& pWork, environment& env)
{
environment::inst() = env;
environment::inst(&env);
return nvidia::minethd::thread_starter(threadOffset, pWork);
}
} // extern "C"
Expand Down
41 changes: 18 additions & 23 deletions xmrstak/misc/environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,30 @@ class params;

struct environment
{

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?

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

else
env = init;
}

environment() : pPrinter(nullptr), pglobalStates(nullptr)
{
return *env;
}

environment()
{
}

printer* pPrinter;
globalStates* pglobalStates;
jconf* pJconfConfig;
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.

globalStates* pglobalStates = nullptr;
jconf* pJconfConfig = nullptr;
executor* pExecutor = nullptr;
params* pParams = nullptr;
};

} // namepsace xmrstak