singleton class misuse, c++ - explanation needed -
i came across tought wasn't possible in c++ (i learning it)
i called nonstatic function without initializing class(singleton class) object or wtf happend, im not grasping it, == null , way did -
my singleton class example:
class lightslogic { public: void lightslogic::request_color_shift_group_after_x_msecs(blabla); int current_used_chase_list = 0; // singleton class thing below private: lightslogic() { // constructor here ! }; lightslogic(lightslogic const&); // don't implement. // singleton void operator=(lightslogic const&); // don't implement // singleton public: static lightslogic& getinstance() // return reference. // singleton { static lightslogic instance; return instance; } };
so somewhere defined:
static lightslogic* logicofligths;
and called method class
logicofligths->request_color_shift_group_after_x_msecs(blabla);
now happend - method used variable:
void lightslogic::request_color_shift_group_after_x_msecs(blabla) { current_used_chase_list; // doing variable //but since variable defined , initialized in class header // , == null, method cant acces variable ?! thinks can ?! //we crash saying: first-chance exception @ 0x013f5e8b in myexe.exe: 0xc0000005: access violation reading location 0x00028d48. // , if use check before accesing variable : if (this == null) { report("this null"); return; } //this prevent crash. }
now correct way of accesing method of singleton class without ruining is:
(&lightslogic::getinstance())->request_color_shift_group_after_x_msecs(blabla); //i know use lightslogic::getinstance(). im using accesing variables, more clear me , compiler should fix misery on compile ?!
why able , doing wrong ? or isnt case of doing wrong , misused memory , getting 'undefined behaviour' ? cause first tought.
the funny part - application works if dont use variables defined in class header of method.
calling via nullpointer has undefined behavior.
anything or nothing can happen.
Comments
Post a Comment