Solved

Getting a CBitmap from a HBITMAP, using FromHandle()

Posted on 1998-07-09
4
4,374 Views
Last Modified: 2013-11-19
I need to get a CBitmap from a HBITMAP, and to be able to delete my CBitmap again, when I need to.

My code looks like this:

class SomeClass
{
   CBitmap *m_bmpBitmap;

   SomeFunction();
   ~SomeClass();
};


SomeClass::SomeFunction()
{
HBITMAP hBitmap;

hBitmap=(HBITMAP) LoadImage(NULL, "BitmapName.Bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
m_bmpBitmap=new CBitmap();
m_bmpBitmap=CBitmap::FromHandle(hBitmap);
}

SomeClass::SomeClass()
{
   delete m_bmpBitmap;
}

My problem is that when I come to the Destructor, and want to delete the m_bmpBitmap again, my program crashes, becourse the CBitmap is attached to a HBITMAP, and I don't know how to detach it again.

How do delete my m_bmpBitmap in a proper way (So that it won't cause the program to crash) ?
0
Comment
Question by:Morten from Eliten
  • 2
4 Comments
 
LVL 3

Expert Comment

by:Norbert
ID: 1318893
Not rearly clear what your problem is but
You should initialisize pointers to 'valid' value
Constructor:
SomeClass::SomeClass()
{
    m_bmpBitmap=NULL;
}
Destructor:
SomeClass::~SomeClass()
{
  if(m_bmpBitmap)
         delete m_bmpBitmap;
}
now SomeFunction
SomeClass::SomeFunction()
{
HBITMAP hBitmap;

hBitmap=(HBITMAP) LoadImage(NULL, "BitmapName.Bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
//m_bmpBitmap=new CBitmap(); NOT NEEDED and Produces MEMORY LEAKS !!!!
if(m_bmpBitmap)
    delete m_bmpBitmap;
m_bmpBitmap=CBitmap::FromHandle(hBitmap);
}

Some OtherFunction
SomeClass::OtherFunction()
{
     if(bmpNoMoreNeeded)
    {
        if(m_bmpBitmap)
        {
            delete m_bmpBitmap
            m_bmpBitmap=NULL;
        }
    }
}

that should prevent double deleting the same pointer
0
 
LVL 23

Expert Comment

by:chensu
ID: 1318894
Regarding Norbert's code, don't delete m_bmpBitmap returned by CBitmap::FromHandle. It will be deleted automatically at idle time. So, the pointer returned by CBitmap::FromHandle may be invalid outside SomeClass::SomeFunction(). You can do it in this way.

class SomeClass
{
   CBitmap *m_bmpBitmap;

   SomeFunction();
   ~SomeClass();
};


SomeClass::SomeFunction()
{
HBITMAP hBitmap;

hBitmap=(HBITMAP) LoadImage(NULL, "BitmapName.Bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
m_bmpBitmap=new CBitmap();
m_bmpBitmap->Attach(hBitmap);
}

SomeClass::~SomeClass()
{
   ::DeleteObject(m_bmpBitmap->Detach());
   delete m_bmpBitmap;
}

To be safer, remember to check the return value of each function called.
0
 

Author Comment

by:Morten from Eliten
ID: 1318895
Hey, chensu !
Propose that as an answer, and you've earned yourself 30 points.

That was great !
0
 
LVL 23

Accepted Solution

by:
chensu earned 30 total points
ID: 1318896
Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl Awk Need Help 3 109
fizzArray  challenge 1 58
only14 challenge 19 68
firstChar challenge 13 107
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now