Acceptable way to do this?

This is a discussion on Acceptable way to do this? within the Windows Programming forums, part of the Platform Specific Boards category; Wondering if anyone could give this a once over to make sure I'm preventing memory leaks, this function gets called ...

  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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21