🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #17058

Fixing a memory leak

#17058
From: Terry
Date: Fri, 11 Apr 2003 17:41
60 lines
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.
Technical Details
Message-ID: <3e96fe40$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-04!sn-xit-01!sn-xit-09!supernews.com!iad-peer.news.verio.net!news.verio.net!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,23a77f481a630239,start
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-04-11 10:49:08 PST
Reply-To: "Terry" <terry@helloworld.com>
From: "Terry" <terry@helloworld.com>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Fixing a memory leak
Date: Fri, 11 Apr 2003 12:41:34 -0500
Lines: 62
Organization: here
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
NNTP-Posting-Host: 66.142.144.216
Message-ID: <3e96fe40$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1050082880 66.142.144.216 (11 Apr 2003 10:41:20 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-04!sn-xit-01!sn-xit-09!supernews.com!iad-peer.news.verio.net!news.verio.net!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail