Coder's Guild Mailing List

Subject: bitmap delete

Posted by Brian Dickey on 2000-12-27

You should use
    delete bmp;

And this is why.  DeleteObject( bmpHandle ) takes a handle to a win32 object
not a pointer to a C++ object.  This function knows nothing about MFC.  The
CBitmap class if designed correctly will call DeleteObject on the handle of
the bitmap that it contains.

Hope this helps

Brian Dickey
----- Original Message -----
From: <tcg@xxxx.xx.xx>
To: <bdickey@xxxxxxxxxxx.xxx>
Sent: Saturday, December 23, 2000 6:55 PM
Subject: TCG digest 901


> Date: Fri, 22 Dec 2000 17:51:53 -0600
> From: "Fox" <sfox@xxxxxxxxxx.xxxxxxxxxxx.xxx>
> Subject: bitmap delete
>
> Language:  MFC C++
> Compiler:  MSVC++ 6
>
> In a class, I have a bitmap pointer:
>
> CBitmap *bmp;
>
> In that program I assign it like so:
>
> bmp = new CBitmap;
>
> I fill it up with
>
> bmp->CreateCompatableBitmap(...);
> bmp->CreateBitmapIndirect(...);
> etc....
>
> Now when I'm done with it what do I delete?  Do I use:
>
> DeleteObject(bmp);
> delete bmp;
>
> Which do I use?  Will I be deleting memory twice if I use both?  To me it
makes
> sense to do both because first you must delete the object (the
'DeleteObject'),
> then the memory allocated for the object (the 'delete bmp')...but is that
true?
>
> No matter which way I've done it, I haven't seen any errors....except that
when I
> use both, menu titles (and other lables) of other windows sometimes turn
big and
> bold.  But I haven't noticed a memory leak when I've used only
one...either one.
> So which way is correct?
>
> Or am I suppose to use:  DeleteObject(*bmp);
>
> Oh, this does bring up something else:  what's the best way to see if a
memory
> leak is happening?  In other words:  How can I be sure that ALL memory
> allocated by my program is being safely freed up?  How does one hunt down
and
> kill a memory leak?  Especially one that doesn't seem to be happening
every time
> the program is run?  I thought I was careful to delete
everything....apparently I was
> wrong.
>
> Any help?  Need more info?  Need more clarification?  Please
reply...thanks.
> > 
>
>