0
//File A.h containing class A
//DLL or dylib code.
class A {
  //Class methods
  A ()
  {
    count = 5;
  }

  //Append is running fine as it is tested
  A& Append (const A& a)
  {
    //Append operation.
    str = str + a.str;
    return *this;
  }       

  //this is running fine in all other cases except for this static object.
  A& operator= (const A& a)
  {
     //Statement 1
     str = a.str;
     //Problem is faced in the statement 1 on the assignment of str to a.str
     //I forget to add this code.
     count = a.count;
     return *this;
  }

  private:
  std::string str;
  int count;
};

//File B.cpp in some other layer
//When these variables in dylib.
static A obj1;
static A obj2;
static void f ();
static void g ();

//This Initialize is called whenver DLL or dylib is being loaded.
Initialize ()
{
   f();
   g();
}

//Problem faced in a function f
void f ()
{
  A a;

  //Some operation performed on a
  a.Append (GetA("String"));

  //Here I am facing problem of Bad memory access possibly over statement 1.

  obj1 = a;
  //Debugger on Windows showing the member of obj1 initialized, but not on Mac OS X.
}

void g ()
{
  A a;

  //Some operation performed on a

  //Here I am facing problem of Bad memory access possibly over statement 1.
  obj2 = a;
  //Debugger on Windows showing the member of obj1 initialized, but not on Mac OS X.
}

//The application An exe or .app on Mac OS X
int main ()
{
   InitializeApplication ();

   void * handle;
   //Dynamic library is being loaded.
   handle = dlopen("mylib.dylib", RTLD_LAZY);

   //Functions are being loaded.
   f1  = dlsym(handle, "MyFunction");

    //Rest of the code.

}

When I run a similar program on Windows (compiled using cl compiler), the value obj1.count and obj2.count are 5 (as initialized by default constructor).

However, when I run this program on Mac OS X (compiled using clang compiler), the value of obj1.count and obj2.count are 0.

Am I missing something to initialize static object of a class? What are the steps required if there is an array?

In my program, there is an application loading a dylib (on Mac OS X) or DLL (on Windows). This code is the part of shared library or DLL.

Static object obj1 and obj2 are in DLL. This DLL is being loaded and then called.

Following behaviour is observed in Windows

  1. Breakpoints put in static class declaration of obj1 and obj2 are hit.
  2. Object of obj1 and obj2 are initialized properly.
  3. Breakpoint put in the constructor are also hit.

On Mac OS X

  1. Breakpoint on declaration and in the constructor are not hit due to this static declaration.
  2. Object obj1 and obj2 are not initialized.

On Mac OS X, everything in the object is initialized by Zero. Every address is NULL.

However, when I moved these variables to a static library (which is linked to this dylib), then everything is running as per the expecation.

Is there any issue of global/static objects in dylib?

doptimusprime
  • 9,115
  • 6
  • 52
  • 90
  • Just run it. In my case on Mac OS X, static object obj1 and obj2 are not initialized. However same were initialized in Windows as per default constructor. Because of this, I am facing the problem of bad memory access. – doptimusprime May 06 '13 at 10:26
  • By `obj1 = a;`, both objects should have been initialized. – Eric Z May 06 '13 at 10:29
  • @Eric, I expect the same. This work fine in Windows. But I am facing problem on Mac OS X where I see in the debugger that these objects are not initialized. – doptimusprime May 06 '13 at 10:31

1 Answers1

1

Since your:

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

doesn't copy count, then we can expect "undetermined" value of count in the copied object. Which may of curse be 5, but also some other value. The operator= should copy (or otherwise initialize) ALL the contents of the class.

Edit: And you should have a return *this; in there too.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Thanks for the answer. I think I am failed to tell the exact problem. I edited the question. That is already being done. Problem is that static objects are not initialized properly before the assignment. – doptimusprime May 06 '13 at 10:44
  • And it does go wrong when you run the program you posted? Or only when in the "real code"? – Mats Petersson May 06 '13 at 10:45
  • In the real code. Here, I posted minimal part of the code. Rest of the code is of production. – doptimusprime May 06 '13 at 10:47
  • I would suggest that you reduce your production code to a minimum, obfuscate the names and post that. There may be something to it that is different from what you have posted - as I can't see anything directly wrong. – Mats Petersson May 06 '13 at 10:50
  • I think this the code to a minimum. The main question why the breakpoints put into the static object declaration and hence in the constructor definition are not hit. – doptimusprime May 06 '13 at 11:03
  • No, that's not what I meant. Does the code you posted actually show the problem? – Mats Petersson May 06 '13 at 11:04
  • Yes. Problem is shown in the code. Question is that why the static objects are not initialized. Even if they get initialized, I have not written any code to set them to zero. Also, breakpoints put into the constructor are not hit. It means constructor for this static object is not called. – doptimusprime May 06 '13 at 11:07
  • After moving these static variables to a static libary (.a), everything is running fine. Is there any issue with static variables in dylib? – doptimusprime May 07 '13 at 07:00