|
-
February 19th, 2009, 10:43 PM
#1
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
-
February 19th, 2009, 10:54 PM
#2
Re: Memory Leak
 Originally Posted by funkmonkey
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.
-
February 19th, 2009, 11:18 PM
#3
Re: Memory Leak
 Originally Posted by Lindley
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......
-
February 20th, 2009, 12:06 AM
#4
Re: Memory Leak
 Originally Posted by funkmonkey
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"
-
February 20th, 2009, 12:09 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|