Initialize / Uninitialize static variables problem

Discussion in 'C++' started by Timothy Madden, Sep 9, 2004.

  1. 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
    Timothy Madden, Sep 9, 2004
    #1
    1. Advertising

  2. 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.
    Steven T. Hatton, Sep 9, 2004
    #2
    1. Advertising

  3. "Timothy Madden" <> wrote in message
    news:...
    > 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
    John Harrison, Sep 9, 2004
    #3
  4. "Timothy Madden" <> wrote in message news:<>...
    > 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

    Open source rating and billing engine for communication networks.
    Stephan Br?nnimann, Sep 9, 2004
    #4
  5. "John Harrison" <> wrote in message
    news:...
    >
    > "Timothy Madden" <> wrote in message
    > news:...
    > > 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.


    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
    Timothy Madden, Sep 14, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SaravanaKumar
    Replies:
    6
    Views:
    9,386
    Tony Morris
    Oct 19, 2004
  2. joe

    passing an uninitialize pointer

    joe, Jul 5, 2004, in forum: C Programming
    Replies:
    20
    Views:
    632
    Richard Bos
    Jul 8, 2004
  3. Neviton
    Replies:
    9
    Views:
    453
    Alf P. Steinbach
    Sep 17, 2007
  4. Martin Wells

    Static inline functions with static local variables

    Martin Wells, Oct 6, 2007, in forum: C Programming
    Replies:
    10
    Views:
    702
    Army1987
    Oct 8, 2007
  5. Bob Rossi

    uninitialize PyEval_InitThreads()

    Bob Rossi, Jun 26, 2012, in forum: Python
    Replies:
    0
    Views:
    299
    Bob Rossi
    Jun 26, 2012
Loading...

Share This Page