Initialize / Uninitialize static variables problem

T

Timothy Madden

Hello everybody !

I have a function in a dll that will write bytes of data in a log file.
The function has a local static FILE pointer like this:

void VMLogPacket(BYTE *pData, size_t nSize)
{
static FILE *dbg = fopen("pakets.log", "wbc");

if (dbg)
{
fwrite(...);

Currently I call _fcloseall() from the ExitInstance member function of my
application class, but this is not a very good aproach because the app has
to do cleanup for the internal work of the dll, beacause _fcloseall() is not
really a standard library function and I'm not sure if it actually
dealocates the FILE structure and because I might not want to close all
files after all.

My question is what is the right way to uninitialize the dbg file, that is
to close it ?
 
S

Steven T. Hatton

Timothy said:
Hello everybody !

I have a function in a dll that will write bytes of data in a log file.
The function has a local static FILE pointer like this:

void VMLogPacket(BYTE *pData, size_t nSize)
{
static FILE *dbg = fopen("pakets.log", "wbc");

if (dbg)
{
fwrite(...);

Currently I call _fcloseall() from the ExitInstance member function of my
application class, but this is not a very good aproach because the app has
to do cleanup for the internal work of the dll, beacause _fcloseall() is
not really a standard library function and I'm not sure if it actually
dealocates the FILE structure and because I might not want to close all
files after all.

My question is what is the right way to uninitialize the dbg file, that is
to close it ?

My question is why do you have the file allocated as a function local static
variable? Why not have a class that creates and initializes the file using
a constructor, makes the file available where it is needed, and deallocates
the file, closing it, etc., in the destructor? That /is/ the C++ way that
I learned from TC++PL(SE). If it's implemented as a member rather than a
member pointer, it will be created and deallocated automatically at startup
and shutdown. That may be too inflexible, but the alternative of using a
pointer can be handles in a similar manner. I just takes a bit more
explicit action on your part.
 
J

John Harrison

Timothy Madden said:
Hello everybody !

I have a function in a dll that will write bytes of data in a log file.
The function has a local static FILE pointer like this:

void VMLogPacket(BYTE *pData, size_t nSize)
{
static FILE *dbg = fopen("pakets.log", "wbc");

if (dbg)
{
fwrite(...);

Currently I call _fcloseall() from the ExitInstance member function of my
application class, but this is not a very good aproach because the app has
to do cleanup for the internal work of the dll, beacause _fcloseall() is not
really a standard library function and I'm not sure if it actually
dealocates the FILE structure and because I might not want to close all
files after all.

My question is what is the right way to uninitialize the dbg file, that is
to close it ?

Put the FILE* in a class, close the file in the class destructor.

Alternatively use the std::eek:fstream class where this work has already been
done for you.

john
 
S

Stephan Br?nnimann

Timothy Madden said:
Hello everybody !

I have a function in a dll that will write bytes of data in a log file.
The function has a local static FILE pointer like this:

void VMLogPacket(BYTE *pData, size_t nSize)
{
static FILE *dbg = fopen("pakets.log", "wbc");

if (dbg)
{
fwrite(...);

Currently I call _fcloseall() from the ExitInstance member function of my
application class, but this is not a very good aproach because the app has
to do cleanup for the internal work of the dll, beacause _fcloseall() is not
really a standard library function and I'm not sure if it actually
dealocates the FILE structure and because I might not want to close all
files after all.

My question is what is the right way to uninitialize the dbg file, that is
to close it ?

A good habit: never call functions that start with an _. Commenly such names
are used internally by the C++ implementations.

Try to think C++ (I guess that why you posted to this group) and not C:
Provide a class VMLog and give it a member std::eek:fstream and open the stream
whenever suitable. The stream will be closed in the destructor.
Also, you may want to make WMLog a singleton class in which case you'll
have to provide a function to close the log file.

class VMLog {
public:
// open the logfile
static bool open(const char* path);
// close the logfile
static void close();
// write to the log
static void write(BYTE *pData, size_t nSize);
private:
// access to logfile, watch out for thread-safety!
static VMLog& instance();
// pointer to the only instance of the class
static VMLog* instance_;
private:
// prevent public construction
VMLog();
// prevent copying: not implemented
VMLog(const VMLog&);
private:
std::eek:fstream logfile_;
};

And then:
void foo()
{
VMLog::write("abcd", 4);
}

Stephan Brönnimann
(e-mail address removed)
Open source rating and billing engine for communication networks.
 
T

Timothy Madden

John Harrison said:
Put the FILE* in a class, close the file in the class destructor.

Alternatively use the std::eek:fstream class where this work has already been
done for you.

I don't have to compose a class for this, there are simpler ways since my
app

is likely to already have initialization and termination functions.

I just faced the same problem in different circumstances

I wanted to create a Win32 semaphore in a static global variable like this:

static HANDLE g_hThreadSem = ::CreateSemaphore(NULL, 1, 1, NULL);

The semaphore may be needed at all times during the execution.

The problem is Win32 requires me to call ::CloseHandle(g_hThreadSem) at

some time before my application exits. My HANDLE is static; I don't want

to put it in the header file and make it available in all modules who don't

need it, including the one with CWinApp::ExitInstance() function.

I would like the semaphore closed automaticaly at program termination, the
same

way it way created upon startup.

I would be happy with an ISO C++ termination function specific for each
translation

unit or any other standard mechanism better than that. Has anyone faced this
little

problem and thinks the same way I do ?

Thank you

Timothy Madden
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top