Hi

I have encountered a problem where I cannot think of a solution.
I'm attempting to work with event tracing but the problem is that a handle is not released even when the program exit, and the API appears such that once it is opened or started, it is closed for business and I have to restart my machine to work on my code again.

As you can imagine this is unaccepable in any case and worse when you know next to nothing about the API and debugging a lot.

Problem is that I'm working with a C++ console app for the moment VS2010 so I need a way to know when user (me) for now closes the console window so I can dispose of the handle cleanly.

The offending API is one of the below, I'm not sure which.
StartTrace or OpenTrace function.

If it were a window app I could close it with a button, but of course I'm so dizzy from investigating this API that I cannot think straight.

Please help, and thanks for reading.

I've ashamedly kbhit() used from #include <conio.h>

while (true)
    {
        Sleep(10);
        if (kbhit())
        {
            // Cleanup
            exit(0);
        }
    }

I am so very open to better suggestions.

reading the answer here it looks like you can register an event handler with the OS and handle the closing of the cmd window youself. in the closing routine you could cleanly close out the API so it usable the next time.

Do you have a complete example that's short enough to post?

You may be able to use some sort of RAII-like approach; i.e., release the handle in some object's destructor. But beware std::exit... nothing with automatic storage duration will have its destructor called. std::atexit should help there.

If possible, I'd avoid "exit" and just set a flag instead:

bool running = true;
do
{
    Sleep(10);
    if (kbhit())
    {
        running = false;
    }
}
while (running)
// Cleanup
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.