469,904 Members | 1,956 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,904 developers. It's quick & easy.

Global instance crash

Is it possible that construction of a global instance can fail?
I'm suspecting this because the program seems to crash before
main(). Is it a better practice to declare a pointer for the
object and then use 'new' to create an instance?

Jul 23 '05 #1
4 1549

"Krice" <pa****@mbnet.fi> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Is it possible that construction of a global instance can fail?
I'm suspecting this because the program seems to crash before
main(). Is it a better practice to declare a pointer for the
object and then use 'new' to create an instance?


It's better practice to write code that doesn't crash. :-)

(Sorry, couldn't resist.)

Construction of an object certainly can fail. Likely reasons are undefined
bahavior exhibited by your constructor, or making assumptions about the
_order_ in which such global objects are constructed.

You should probably debug the software, setting breakpoints in any
constructors or other code that might get called during startup.

But simply switching to dynamic allocation isn't neccessarily going to help
at all. It might, but then again it might not. Or, it might appear to
work, but only mask the real problem. Better to find out exactly why it's
crashing.

Some might suggest never using a global instance of anything. I'd suggest
_avoiding_ global instances, but there are cases where it's perfectly
appropriate.

-Howard


Jul 23 '05 #2
Krice wrote:
Is it possible that construction of a global instance can fail?
Of course.
I'm suspecting this because the program seems to crash before
main(). Is it a better practice to declare a pointer for the
object and then use 'new' to create an instance?


Outside of making it a little bit easier to trace what may have gone
wrong, there's little difference. In general, though, globals of any
sort are to be avoided unless you have a good reason for them -- and you
may.

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Jul 23 '05 #3
Howard wrote:
Likely reasons are undefined bahavior exhibited by your constructor,
or making assumptions about the _order_ in which such global objects
are constructed.
I was calling Rebuild() in the constructor of class Place, but it
was using data from class Cave and that wasn't initialized yet..
FAQ 10.12 says: "I hear they're hiring down at McDonalds. Enjoy
your new job flipping burgers.":)
Some might suggest never using a global instance of anything.


It's so tricky to pass variables around.. for example I'm using
class Cave to contain some common data about the current level,
like width and height: Cave.W; and Cave.H; It's easy to use
those variables everywhere:)

Jul 23 '05 #4
Krice wrote:
Is it possible that construction of a global instance can fail?
I'm suspecting this because the program seems to crash before
main(). Is it a better practice to declare a pointer for the
object and then use 'new' to create an instance?


The best way to avoid this IMO is to use a singleton pattern instead of
a global.

class MySingleton
{
public:
static MySingleton* GetInstance();
private:
static MySingleton* instance;
};

MySingleton* MySingleton::instance = 0;

MySingleton* MySingleton::GetInstance()
{
if(!instance)
instance = new MySingleton;
return instance;
}

This can still cause trouble if the constructor of MySingleton calls
anything that calls GetInstance(), but if that's the case then you
should review your design and figure out who should be calling who.

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Jean-Marc Blaise | last post: by
8 posts views Thread by Lana Zapornikova | last post: by
2 posts views Thread by Mike | last post: by
7 posts views Thread by zeecanvas | last post: by
10 posts views Thread by ma | last post: by
8 posts views Thread by yinglcs | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.