Solved

Building a destructive function.

Posted on 1999-01-20
5
154 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
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

685 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