Thread View: borland.public.cppbuilder.nativeapi
6 messages
6 total messages
Started by Terry
Fri, 11 Apr 2003 17:41
Fixing a memory leak
Author: Terry
Date: Fri, 11 Apr 2003 17:41
Date: Fri, 11 Apr 2003 17:41
60 lines
2419 bytes
2419 bytes
I have a function that i am calling like this
int FileSize;
PeopleType *People = (PeopleType*)ReadFileToBuffer("Database.dat",
&FileSize);
//--------------------------------------------------------------------
-------
char* TForm1::ReadFileToBuffer(char *FileName, int *FileSize)
//
// Returns:
// CharPointerToData if Successful
// NULL if Failed
{
HANDLE FileHandle = CreateFile(FileName, GENERIC_READ,
FILE_SHARE_READ,
NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,
NULL);
if(FileHandle == INVALID_HANDLE_VALUE || FileHandle == NULL) return
NULL;
*FileSize = (int)GetFileSize( FileHandle, NULL );
if( *FileSize == 0xFFFFFFFF )
{
CloseHandle( FileHandle );
return NULL;
}
char* pBuffer = new char[*FileSize];
DWORD NumberOfBytesRead;
if(ReadFile(FileHandle, (void*)pBuffer, (DWORD)*FileSize,
&NumberOfBytesRead, NULL) == 0)
{
CloseHandle( FileHandle );
delete[] pBuffer;
return NULL;
}
CloseHandle( FileHandle );
return pBuffer;
}
//--------------------------------------------------------------------
-------
The problem is when i create the pBuffer here, i get a resource leak
because i am
not deleting the pBuffer. Can someone give me some advice on how to
delete this
so i don't get a memory leak. I tried deleting in the form
OnDestroy(), but that
does no good.
Thanks.
Re: Fixing a memory leak
Author: JD
Date: Fri, 11 Apr 2003 17:50
Date: Fri, 11 Apr 2003 17:50
13 lines
1124 bytes
1124 bytes
Try this:
PeopleType *People = (PeopleType*) ReadFileToBuffer "Database.dat", &FileSize);
if( People )
{
// use People;
delete People;
}
BTW: It is permissable to delete a NULL pointer in C++
~ JD
Re: Fixing a memory leak
Author: Remy Lebeau (TeamB)
Date: Fri, 11 Apr 2003 19:35
Date: Fri, 11 Apr 2003 19:35
26 lines
1525 bytes
1525 bytes
"JD" <nospam@nospam.com> wrote in message
news:3e970049$1@newsgroups.borland.com...
> Try this:
<snip>
That is not valid. The memory was allocated as a char[] but then you're
freeing it as a People* instead. They must match. So the memory has to be
freed as a char* using delete[] instead:
char *pBuf = ReadFileToBuffer("Database.dat", &FileSize);
if( pBuf )
{
PeopleType *People = (PeopleType*)pBuf;
// use People as needed
delete[] pBuf;
}
> BTW: It is permissable to delete a NULL pointer in C++
Yes.
Gambit
Re: Fixing a memory leak
Author: Terry
Date: Fri, 11 Apr 2003 20:15
Date: Fri, 11 Apr 2003 20:15
20 lines
1424 bytes
1424 bytes
Thanks JD, let me give it a shot.
"JD" <nospam@nospam.com> wrote in message
news:3e970049$1@newsgroups.borland.com...
>
> Try this:
>
> PeopleType *People = (PeopleType*) ReadFileToBuffer
"Database.dat", &FileSize);
> if( People )
> {
> // use People;
> delete People;
> }
>
> BTW: It is permissable to delete a NULL pointer in C++
>
> ~ JD
>
Re: Fixing a memory leak
Author: Terry
Date: Fri, 11 Apr 2003 23:24
Date: Fri, 11 Apr 2003 23:24
22 lines
1635 bytes
1635 bytes
Great, thanks a lot, no more codeguard errors.
"Remy Lebeau (TeamB)" <gambit47@yahoo.com> wrote in message
news:3e971900$1@newsgroups.borland.com...
>
> > Try this:
> <snip>
>
> That is not valid. The memory was allocated as a char[] but then
you're
> freeing it as a People* instead. They must match. So the memory
has to be
> freed as a char* using delete[] instead:
>
> char *pBuf = ReadFileToBuffer("Database.dat", &FileSize);
> if( pBuf )
> {
> PeopleType *People = (PeopleType*)pBuf;
> // use People as needed
>
> delete[] pBuf;
> }
Re: Fixing a memory leak
Author: JD
Date: Sat, 12 Apr 2003 01:30
Date: Sat, 12 Apr 2003 01:30
18 lines
1300 bytes
1300 bytes
"Remy Lebeau \(TeamB\)" <gambit47@yahoo.com> wrote: > >"JD" <nospam@nospam.com> wrote in message >news:3e970049$1@newsgroups.borland.com... > >> Try this: ><snip> > > That is not valid. The memory was allocated as a char[] but > then you're freeing it as a People* instead. > They must match. I did not know that (obviousely) ~ JD
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads