Solved

Building a destructive function.

Posted on 1999-01-20
5
149 Views
Last Modified: 2010-05-18
I need to build a fuction that will help me exit in a 'clean' way from my program. I need it to:
*Exit ALL threads, even if it is called from a grandson thread - it should kill his grandpa and all the grandpa's sons..
*close some hardware devices that are opened in some other place in the program.
*Destroy a taskbar icon opened somewhere else in the program
*Close a mutex handle created somewhere else in the program

and so on...
Is there a nice way of doing all these actions (without having handles/pointers to all the objects mentioned above)?

Thanks!
0
Comment
Question by:shimison
  • 4
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
Comment Utility
The best way to do that is to use classes to maintain the "window objects"  When the C++ objects are destroyed, the windows objects will be closed/destroyed by the destructor.

details follow.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
For example, you can use a file class to maintain window file handles.  The class's destructor would automatically close the file.  Like

class FileClass
{
   HFILE Hnd;
public:
    FileClass(HFILE H) : Hnd(H) {}:
    ~FileClass() { if (Hnd) CloseFile(H); }:
}

obvously other members would be needed to make this useful.  The way this thing works is that you store your file handles inside these objects (Actually, you should develope a nice little library that allows you to use these objects in place of file handles, that is, let it deal with file handles and you deal with it.) If you declare on of these objects local to a procedure, the file will be closed automatically when the procodure ends (even if it ends with an exception).  If you declare one globally, the file will be closed automaticall when the program ends--again even with an exception.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Now this sort of approach insures that your application will clean itself up when it exits.  If you want to kill your application (and clean up in the process) just throw an exception of some type that isn't caught.  With this method though it also cleans up as needed when an exception is thrown and caught or when no exception is thrown.  

A similar improvement to consider is to use smart pointers.   These help insure that memory allocated will be freed automatically.
0
 

Author Comment

by:shimison
Comment Utility
Thanks.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
You're welcome.  good luck.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now