473,687 Members | 3,586 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1688

"Krice" <pa****@mbnet.f i> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.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::in stance = 0;

MySingleton* MySingleton::Ge tInstance()
{
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
4610
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
3139
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 db2diag.log like: ================= 2004-06-06-17.21.48.671000 Instance:DB2 Node:000
2
5560
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 brings the whole instance down and it has to be restarted. In addition the job seems to be stuck in the TOOLSDB as if I try to delete the backup job I get "The task is currently running". So I went to the command line and tried "db2 backup database...
33
3270
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. I think the use of globals is worrisome in this case. Anybody have any experience with Access on Citrix? (Al Kallal already griped me out about globals, but if he has anything new to add I'm all ears).
7
2573
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) global variables outside of the files they were defined it, and created some files that just hold global variables definitions (just variables, without any function definition). So, depending on the purpose/category of variables, they're defined...
7
1680
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 custom 500 handler in Web.Config 3. Specify a custom 500 handler in IIS
10
3574
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 = "hello world";
8
3448
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 so that it can be set/get by other files? 2. If I have multiple instance of the same program running in the same machine (linux), will each program instance has its own of global
1
29355
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 called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
0
8590
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8527
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9070
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8781
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8784
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4321
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2964
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1948
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.