CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Memory Leak

  1. #1
    Join Date
    Feb 2009
    Posts
    35

    Memory Leak

    Hi,

    I have not been able to find out how to get around the memory leak. I know what part of the code is giving me problems, but have yet found a way to resolve it.

    Here is the code in question:

    #include <sys\types.h>
    #include <sys\stat.h>
    __int64 FileSize64( const char * szFileName )
    {
    struct __stat64 fileStat;
    int err = _stat64( szFileName, &fileStat );
    if (0 != err) return 0;
    return fileStat.st_size;
    }



    char *string = new char[szTemp.GetLength() + 1];
    string = (char*)szTemp.GetBuffer(szTemp.GetLength() + 1);
    szTemp.ReleaseBuffer();
    wcstombs(string, szTemp, szTemp.GetLength() + 1);
    m_nFileSize = FileSize64(string);

    *** Blows up right here ***
    delete [] string;

    Everything I have tried has not worked. What in the world am I doing wrong and how can I fix it?

    TIA

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Memory Leak

    Quote Originally Posted by funkmonkey View Post
    char *string = new char[szTemp.GetLength() + 1];
    string = (char*)szTemp.GetBuffer(szTemp.GetLength() + 1);
    Right there, that's the problem. The newly allocated char array is lost as soon as you assign something else to "string".

    I also find it very questionable that you're trying to use szTemp after calling ReleaseBuffer. But without knowing what type of thing that is or what its semantics are, it's hard to say for sure.
    Last edited by Lindley; February 19th, 2009 at 11:13 PM.

  3. #3
    Join Date
    Feb 2009
    Posts
    35

    Re: Memory Leak

    Quote Originally Posted by Lindley View Post
    Right there, that's the problem. The newly allocated char array is lost as soon as you assign something else to "string".

    I also find it very questionable that you're trying to use szTemp after calling ReleaseBuffer. But without knowing what type of thing that is or what its semantics are, it's hard to say for sure.
    Great, but how do I resolve it?

    The only thing the szTemp string is doing is allowing me to pass the file name to the string. I don't use it "after" this at all. It is only used as a "copy" of the file that I want information about.

    This was the only way I found to pass a char* string to the function parameter of (const char* string)

    If you have other suggestions, I'm all ears......

  4. #4
    Join Date
    Feb 2005
    Location
    Pune (India)
    Posts
    644

    Thumbs up Re: Memory Leak

    Quote Originally Posted by funkmonkey View Post


    char *string = new char[szTemp.GetLength() + 1];
    string = (char*)szTemp.GetBuffer(szTemp.GetLength() + 1);
    szTemp.ReleaseBuffer();
    wcstombs(string, szTemp, szTemp.GetLength() + 1);
    m_nFileSize = FileSize64(string);
    HI,

    try this


    char *string = NULL;//new char[szTemp.GetLength() + 1];
    string = (char*)szTemp.GetBuffer(szTemp.GetLength() + 1);
    wcstombs(string, szTemp, szTemp.GetLength() + 1);
    m_nFileSize = FileSize64(string);
    szTemp.ReleaseBuffer();
    string = NULL;

    I think there is no need to allocate the memory as getBuffer itself return ptr to string buffer.

    here I am assuming you are using CString szTemp.

    -Anant
    "Devise the simplest possible solution that solves the problems"

  5. #5
    Join Date
    Feb 2009
    Posts
    35

    Re: Memory Leak

    GREAT!!!!

    Thanks, that worked like a charm.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured