Re: Re[2]: C++ Constructors on global vars not being run ?
Re: Re[2]: C++ Constructors on global vars not being run ?
- Subject: Re: Re[2]: C++ Constructors on global vars not being run ?
- From: Andy O'Meara <email@hidden>
- Date: Mon, 24 Sep 2007 14:00:58 -0400
The whole startup/shutdown dilemma is of course a common one.
Because of the minefield that is static de-initialization (and it's a
*huge*, *ugly* minefield, I might add), I recommend the following
pattern (disclaimer: the implementation of the following in our
codebase is fancier and safer, but you get the idea)...
class BaseObject {
public:
virtual ~BaseObject() { }
};
...
int gNumSingletons = 0;
BaseObject* gSingletonList[ MAX_SINGLETONS ] = { 0, ... }
void main() {
RunApp();
// Shut everything down in the reverse order is was started
for ( i = gNumSingletons - 1; i >= 0; i-- )
delete gSingletonList[ i ];
}
...
extern gNumSingletons, gSingletonList, etc...
class MySingleton : public BaseObject {
public:
// Pre: call Startup() at least once before using sSingleton.
static MySingleton* sSingleton = NULL;
// Whoever uses MySingleton just needs to make sure they call
MySingleton::Startup() before using sSingleton.
static void Startup() {
if ( sSingleton == NULL ) {
sSingleton = new MySingleton;
gSingletonList[ gNumSingletons ] = sSingleton;
gNumSingletons++;
}
private:
MySingleton() {
// Do startup/init code here
}
~MySingleton() {
// Do shutdown stuff here
}
}
};
The above pattern also buys you "lazy" initialization if you so
desire, allowing your app to conserve resources for code/modules that
aren't needed for that session.
-Andy
On Sep 24, 2007, at 11:29 AM, Peter Mulholland wrote:
Thanks for your comments.. i know it seems really odd...
Anyway, what I found out was that the class was being defined in a
seperate file to the main file. There were no other links between main
and this file... at some point the linker decided, as there were no
links between the two, it wouldnt bother linking it!
The deal was this - I'm porting some code which is part of a toolkit.
This uses a C++ class constructor to "register" init/run/uninit
functions of a main app with the toolkit. For some reason, Xcode
decided that as there were no direct calls between the toolkit's code
and the app's code, it wouldn't bother linking, and that's why the
constructor never ran.
My question now is, how do I force Xcode to link ALL object files
given to it, and not try to be clever ?
--
Best regards,
Peter mailto:email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40soundspectrum.com
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden