c++ - Static object not initialized on Mac OS X -


//file a.h containing class //dll or dylib code. class {   //class methods   ()   {     count = 5;   }    //append running fine tested   a& append (const a& a)   {     //append operation.     str = str + a.str;     return *this;   }           //this running fine in other cases except static object.   a& operator= (const a& a)   {      //statement 1      str = a.str;      //problem faced in statement 1 on assignment of str a.str      //i forget add code.      count = a.count;      return *this;   }    private:   std::string str;   int count; };  //file b.cpp in other layer //when these variables in dylib. static obj1; static obj2; static void f (); static void g ();  //this initialize called whenver dll or dylib being loaded. initialize () {    f();    g(); }  //problem faced in function f void f () {   a;    //some operation performed on   a.append (geta("string"));    //here facing problem of bad memory access possibly on statement 1.    obj1 = a;   //debugger on windows showing member of obj1 initialized, not on mac os x. }  void g () {   a;    //some operation performed on    //here facing problem of bad memory access possibly on statement 1.   obj2 = a;   //debugger on windows showing member of obj1 initialized, not on mac os x. }  //the application exe or .app on mac os x int main () {    initializeapplication ();     void * handle;    //dynamic library being loaded.    handle = dlopen("mylib.dylib", rtld_lazy);     //functions being loaded.    f1  = dlsym(handle, "myfunction");      //rest of code.  } 

when run similar program on windows (compiled using cl compiler), value obj1.count , obj2.count 5 (as initialized default constructor).

however, when run program on mac os x (compiled using clang compiler), value of obj1.count , obj2.count 0.

am missing initialize static object of class? steps required if there array?

in program, there application loading dylib (on mac os x) or dll (on windows). code part of shared library or dll.

static object obj1 , obj2 in dll. dll being loaded , called.

following behaviour observed in windows

  1. breakpoints put in static class declaration of obj1 , obj2 hit.
  2. object of obj1 , obj2 initialized properly.
  3. breakpoint put in constructor hit.

on mac os x

  1. breakpoint on declaration , in constructor not hit due static declaration.
  2. object obj1 , obj2 not initialized.

on mac os x, in object initialized zero. every address null.

however, when moved these variables static library (which linked dylib), running per expecation.

is there issue of global/static objects in dylib?

since your:

  a& operator= (const a& a)   {      //statement 1      str = a.str;   } 

doesn't copy count, can expect "undetermined" value of count in copied object. may of curse 5, other value. operator= should copy (or otherwise initialize) contents of class.

edit: , should have return *this; in there too.


Comments