Solved

Building a destructive function.

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 55
Lambda for random numbers problem 7 113
Need some help with Microsoft Visual Studio C++ 2003 5 61
c++, dynamic object by json 1 42
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 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.

832 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