Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Initialize / Uninitialize static variables problem

Reply
Thread Tools

Initialize / Uninitialize static variables problem

 
 
Timothy Madden
Guest
Posts: n/a
 
      09-09-2004
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 ?

------------------------------------------------------------------------
Thank you
Timothy Madden
And I don't wanna miss a thing


 
Reply With Quote
 
 
 
 
Steven T. Hatton
Guest
Posts: n/a
 
      09-09-2004
Timothy Madden wrote:

> 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.

--
"[M]y dislike for the preprocessor is well known. Cpp is essential in C
programming, and still important in conventional C++ implementations, but
it is a hack, and so are most of the techniques that rely on it. ...I think
the time has come to be serious about macro-free C++ programming." - B. S.

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      09-09-2004

"Timothy Madden" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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:fstream class where this work has already been
done for you.

john


 
Reply With Quote
 
Stephan Br?nnimann
Guest
Posts: n/a
 
      09-09-2004
"Timothy Madden" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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: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:fstream logfile_;
};

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

Stephan Brönnimann
http://www.velocityreviews.com/forums/(E-Mail Removed)
Open source rating and billing engine for communication networks.
 
Reply With Quote
 
Timothy Madden
Guest
Posts: n/a
 
      09-14-2004

"John Harrison" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Timothy Madden" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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: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

------------------------------------------

And I don't wanna miss a thing


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
uninitialize PyEval_InitThreads() Bob Rossi Python 0 06-26-2012 12:51 AM
How can I access non-static variables from static funcition? Neviton C++ 9 09-17-2007 10:21 PM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
Instantiating a static class( Class with all static members - methods and variables) SaravanaKumar Java 6 10-19-2004 08:20 AM
passing an uninitialize pointer joe C Programming 20 07-08-2004 11:36 AM



Advertisments