473,386 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 1670

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Jean-Marc Blaise | last post by:
Hi, I'm on Linux Intel, V8.1 / FP4. I have an instance crash. The test case: db2set DB2_FMP_COMM_HEAPSZ = 0 db2stop db2start db2mtrk -i -d -p -v ==> Instance is crashed.
8
by: Lana Zapornikova | last post by:
We are using DB2 Workgroup Server, v. 8.1, fixpack 5 under Windows 2003 Standart Edition. There were series of similar instance crashes. At the begining, some errors appeared in the...
2
by: Mike | last post by:
Greetings, Having a major problem here. running version 8.2 on win2003 server. The problem I am having is backing up a database seems to get to the last part of the backup and then fails. This...
33
by: DFS | last post by:
An application I wrote has been deployed on Citrix, and the Citrix admin tells me all users run the same .mde file. There aren't a lot of concurrent users, but even 2 could be cause for concern. ...
7
by: zeecanvas | last post by:
Hi, First of all: Yes, I know global variables are bad, but I've a huge amount of legacy code, and I've to maintain it _as_is_. I'm maintaining a big program. I moved all (program-wide scope)...
7
by: Jason Kester | last post by:
Best I can tell, there are three basic ways you can deal with global error handling in ASP.NET. Namely: 1. Derive all your pages from a custom Page class, and override OnError() 2. Specify a...
10
by: ma | last post by:
Hello, I want to create a global class. To do this I did the followings: 1- Create a class name test. It has a public variable named mystring. public class test { public string mystring =...
8
by: yinglcs | last post by:
Hi, I read this article about global variable in c: http://www.phim.unibe.ch/comp_doc/c_manual/C/SYNTAX/glo_int_vars.html But I have a few questions 1. how can I declare the global variable...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.