Article View: borland.public.cppbuilder.nativeapi
Article #7023Re: Memory problems: new versus GlobalAlloc
From: Bob Gonder
Date: Thu, 24 Mar 2005 02:38
Date: Thu, 24 Mar 2005 02:38
42 lines
2435 bytes
2435 bytes
Wim Bekker wrote:
>I will post my project (40KB) in attachments with some more comment.
I can't promise this is a problem, but I have to wonder about it....
>int __fastcall GetBits(Graphics::TBitmap* Bitmap, void* Buffer, int& Size)
>{
> unsigned int InfoHeaderSize;
> unsigned int ImageSize;
> GetDIBSizes(Bitmap->Handle,InfoHeaderSize,ImageSize);
> if ((unsigned int)Size<ImageSize)
> {
> Size=ImageSize;
> return -1;
> }
What if ImageSize is 0?
Then you'll be doing GetDIB into a NULL Buffer.
It is also a wasted call the second time around.
Would be better to make this two functions: GetSize, and GetData, or
incorporate the Buffer allocation into this function, so it only gets
called once.
> TBitmapInfo BitmapInfo;
> int Result=GetDIB(Bitmap->Handle,Bitmap->Palette,&BitmapInfo,Buffer);
This fills in BitmapInfo, and Buffer.
GetDIBSizes() gave you the sizes for both of them, but you only used
the size for Buffer.
It would seem odd that it would give you a size for Info if it was a
set amount. Might be a good idea to allocate the proper size.
> return Result;
>}
> //allocate memory
> if (cxGlobalMemory->Checked) Buffer=(char*)GlobalAlloc(GMEM_FIXED,Size);
> else Buffer=new char[Size];
You never check if new or GlobalAlloc succeed in allocating Buffer.
Technical Details
Message-ID:
<ca9441lbia8eochgsn5f6bltpsjamjf983@4ax.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<42403f3c@newsgroups.borland.com> <42408062$3@newsgroups.borland.com> <42413c01$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,6b3a605b9bc38024 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: Bob Gonder <notbg@notmindspring.invalid> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: Memory problems: new versus GlobalAlloc Date: Wed, 23 Mar 2005 18:38:39 -0800 Organization: MindSpring Enterprises, Inc. Reply-To: notbg@notmindspring.invalid Message-ID: <ca9441lbia8eochgsn5f6bltpsjamjf983@4ax.com> References: <42403f3c@newsgroups.borland.com> <42408062$3@newsgroups.borland.com> <42413c01$1@newsgroups.borland.com> X-Newsreader: Forte Agent 1.93/32.576 English (American) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 67.77.146.221 X-Trace: newsgroups.borland.com 1111631913 67.77.146.221 (23 Mar 2005 18:38:33 -0800) Lines: 45 Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail