Re: Static data - can it go out of scope?
On Jul 5, 8:45 am, Stodge <sto...@gmail.com> wrote:
I'm having an issue with Boost Python & C++. I have a C++ class Entity
exported to Python that includes private static data, which is
basically an array of strings. My class contains a public static
function Dump(), which is also exported to Python using Boost Python.
My application initialises the string array at startup and I've
verified in the debugger that this does happen.
The fun happens when I call the static function Dump from Python:
Entity.Dump()
The function executes ok, but the string array is empty. It's as
though the private static data has gone out of scope or been "lost". I
just read and learned that static data is maintained as a "global
variable". Can this happen in C++? Can static data go out of scope?
Yes, it can go out of scope when it's containing executable module
goes out of scope.
I have never used Boost Python, but my guess is that the exporting
mechanism is based on dynamic libraries. If that is the case, then
this behavior would be expected:
At run time, you have two executables:
1. one for your "application" (a.out or .exe),
2. one for your dynamically-linkable library (.DLL or .so)
The .EXE/a.out will exist as A complete entity at run-time.
The .DLL/.so will exist as a complete entity at run-time.
The global memory for the static array of strings inside the .EXE/
a.out is inside the .EXE/a.out.
The global memory for the static array of strings inside the .DLL/.so
is inside the .DLL/.so
Your function that initializes the array is initializing the one that
is in the .EXE/a.out.
You might have to export another initialization function from the
module that allows you to initialize the static array that is inside
the .DLL/.so
-Le Chaud Lapin-
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]