Thread: static char in function problem

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    11

    static char in function problem

    WARNING: NEWBIE to C! I am porting my programs from VB to C and C++. With my current program, I have a timer that calls the following function (IsFulll()) which calls the GetRecentDocs() function...

    Code:
    char *GetRecentDocs() {
         LPITEMIDLIST pidl;
         if (SHGetSpecialFolderLocation(NULL, CSIDL_RECENT, &pidl) == NOERROR) {
            /* char buffer = new char[MAX_PATH]; Causes Mem Leak */
            /* static char buffer[MAX_PATH]; Causes Mem Leak */
            char buffer[MAX_PATH] = ""; /* Compiler  Local Variable Error and Mem Leak */
            SHGetPathFromIDList(pidl, buffer);
            return buffer;
         }
    }
    
    int IsFull() {
         char *sPath;
         sPath = GetRecentDocs();
         /* sPath = "C:\\Documents and Settings\\chris\\Recent\\*.lnk"; Hard Coded causes no Mem Leaks */
    
         strcat(sPath, "\\*.lnk");
    
         HANDLE hFile = 0;
         WIN32_FIND_DATA wfs;
         hFile = FindFirstFile(sPath, &wfs);
         if (strstr(wfs.cFileName, ".lnk") != NULL) {
                   return 1;
         } else {
                   return 0;
         }
    }
    As noted, my problem is with memory leaks. Mem Usage in the Task manager increases between 4 and 8 bytes. Left the program for an hour and it got up to 17Mb. Any ideas would be greatly appreciated...

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The best way to handle this situation is for the caller to allocate memory. For example:
    Code:
    /*
     * sPath must be a buffer of at least MAX_PATH characters.
     */
    void callee(char sPath[])
    {
        strcpy(sPath, "C:\\Jokes");
    }
    
    void caller(void)
    {
        char sPath[MAX_PATH];
    
        callee(sPath);
    
        printf("The path is %s.\n", sPath);
    }
    If you use dynamic memory, you must release it at some point.
    Code:
    char* callee(void)
    {
        sPath = new char[MAX_PATH];
        strcpy(sPath, "C:\\Jokes");
        return sPath;
    }
    
    void caller(void)
    {
        char *sPath = callee();
    
        printf("The path is %s.\n", sPath);
    
        delete [] sPath;
    }
    The new/delete operators are specific to C++. The equivalent C functions are malloc/free.

    A static buffer does not leak memory. However, it is not re-entrant. This means that since there is only one copy of the buffer, it will be overwritten every time you call the function.

    In this case, I suggest you get rid of the function and use SHGetSpecialFolderPath instead. Also, since the path is unlikely to change, you could retrieve it once at program start up and store it in a global variable.

    The GetRecentDocs function is probably not the source of your memory leak. The biggest memory leak in your code is that you are not calling FindClose on the file search handle.

    Finally, instead of searching a directory repeatedly, I suggest you look into the directory monitoring functions. Start with the FindFirstChangeNotification function.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    11

    I'll give it a try...

    ...and post the results, I appreciate such a quick response.. Thanks!

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    11

    Works Great!!!

    I changed the call to a global, called it on startup, and added the FindClose and have no continually growing memory usage anymore. I appreciate the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Problem with Free.
    By chakra in forum C Programming
    Replies: 9
    Last Post: 12-15-2008, 11:20 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM