I've got this strange problem that the same sources compiled as shared lib segfaults as soon as I try to access a list object but exactly the same sources compiled as process work fine with no problem.!
class C_NameValue
{
public:
string name;
string value;
for (L_Filedef::iterator fd=_filedefs.begin(); fd!=_filedefs.end(); ++fd)
{
if (fd->filename == filename) return; ---> ERROR HERE
}
_filedefs.push_back(filedef);
}
above is compiled as a static lib, no problem. Now when I call addFile from a program and compile it both as a shared lib and a process, the process works fine but the shared lib segfaults as soon as i try to access anything in the list, namely the _filedefs.begin(). gdb shows the fd to be 0x0!! and has a size of 8. When I run the process through gdb it also shows a size of 8 but a valid address for fd.
It sounds like a variable is not getting initialized when it is being linked as a .so.
If there are global variables (in the .so) involved, the question is what causes them to be initialized?
When linked statically, the compiler will just call a thunk as part of the static initialization that occurs prior to main. When linked as a .so, that thunk doesn't exist, so the variable does not get initialized.
Well I've tried calling a public method of ACI_OCD that calls addFile from a simple c program that has no global variables. In fact it does nothing more than return the returnVal from this method. The only difference is, for shared lib call its in a named function, for the process in main. No luck, continues to fail with SIGSEGV on the line above.
Thanks for your help so far, I'll try and see if there any global variables in this static lib I can get rid of. Do let me know if something else occurs to you.
Jesus man jsmith. That was a brilliant hint.! There is this global variable, just one that I had, moved its declaration into a method of ACI_OCD which now returns this variable and voila.! Everything works great. Much appreciated.