Thread: Acceptable way to do this?

  1. #1
    Sindolist
    Guest

    Acceptable way to do this?

    Wondering if anyone could give this a once over to make sure I'm preventing memory leaks, this function gets called ALOT and not
    really thrilled if it starts leaking.

    Code:
    /* Function WriteToLog(), Sets A New String To The Log Message Pointer & Writes To Log File */
    void WriteToLog(TCHAR* pMessage)
    {
    
    	/* Check To Make Sure We Have A New String */
    	if(!pMessage)
    	{
    
    		return;
    
    	}
    
    	/* Reset The Old Pointer Just To Be Sure */
    	if (g_pLogMsg)
    	{
    
    		delete[] g_pLogMsg;
    		g_pLogMsg = NULL;
    
    	}
    
    	/* Assign The New String To The Pointer */
    	g_pLogMsg = new TCHAR[lstrlen(pMessage) + 1];
    	lstrcpy(g_pLogMsg, pMessage);
    
    	/* Write To Log File */
    	WriteFile(g_hLogFile, g_pLogMsg, lstrlen(g_pLogMsg), &g_dwStored, NULL);
    
    	/* Reset The Old Pointer Again */
    	if (g_pLogMsg)
    	{
    
    		delete[] g_pLogMsg;
    		g_pLogMsg = NULL;
    
    	}
    
    	return;
    
    }

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    25
    why do all that instead of just using the pointer to the message to write.. it is already there...
    inZane
    --true programmer's don't comment--
    --programmer wannabes complain about it--

  3. #3
    Sindolist
    Guest
    I just got what you were talking about, originally the function just set the pointer, and after several versions of the function, I added the writefile to the thing just to be practical, I never realized I didn't need the global pointer anyway.
    Thanks for simplifying it for me =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bad practice or acceptable?
    By Noose in forum C++ Programming
    Replies: 6
    Last Post: 06-09-2004, 01:43 AM
  2. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  3. is dev-c an acceptable alternative to djgpp?
    By Waldo2k2 in forum Tech Board
    Replies: 1
    Last Post: 03-20-2003, 01:58 PM