There is something terribly wrong here.
If you do not need to have argc and argv avaliable when first creating object, remove init function completely, make I() handle object creation and use default constructor.
If you need thaem, you should not pass them second time.
In your previous posts you wrote that you cannot use default constructor because you need to passs argc/argv but you do not use them in your latest code.
What exactly do you want? You ask something, I give you an answer, In your following question you do something contradicting your previous statemenst, I modify code to suit your new needs, then you do something completely different again.
I originally used the argument in constructor of MyGlobalClass to parse the arguments of CL. Then you said that the singleton should wrap all (well, i am not sure if this is exactly what you said) so I thought that it should be in the constructor of the singleton. As far as I understand now, before I can call instance() the constructor must finished the process of creation of instance. This means I could not to place the code in construcor of singleton. I needed some time to understand is completely. So now I see how to do it without the need to pass the args to init.
before I can call instance() the constructor must finished the process of creation of instance
Either that or it should create instance inside instance() using default constructor.
To create you singleton you have init() function. You can even add some default arguments to the instance() function making ugly, but working boilerplate around it.
SO there is severall approaches:
1)Use init(): use code you have now. call to CLPars.ParseArgs(argc, argv); should be inside Singleton constructor.
2) Initialize object in default state, then change it state by calling member functions:
Ditch init() function. Make default constructor for your object. Make instance() create new object if there isn't one.
Then call CLPars.ParseArgs(argc, argv); on it. So code will be
1 2 3
int main(int argc, char** argv)
{
S::I()->CLPars.ParseArgs(argc, argv); //No init
3) Make something like
1 2 3 4 5 6 7 8 9
S::I(int argc = 0, char** argv = nullptr)
{
if (!instance_)
if (!argv)
throw logic_error("Invalid initialization");
else
instance_ = new S(argc, argv);
return instance_;
}
And make your class to be initialized in parametrized constructor like in (1).
Usage:
Best approach is to design your class using only one constructor. Make sure everything works and you can create it using this constructor, then make it Singleton. It is easier to work with already working code.
Both printing leaks but I read that this is normal because Singleton stays in memory till the program finishs. I guess that the debug tool is not able to detect whether it is really freed or not.