[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Building a destructive function.

Posted on 1999-01-20
5
Medium Priority
?
158 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 200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

649 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