Solved

Building a destructive function.

Posted on 1999-01-20
5
156 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1184603
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
ID: 1184604
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
ID: 1184605
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
ID: 1184606
Thanks.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1184607
You're welcome.  good luck.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

688 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