0

C++/CLI アプリケーションで PictureBox の画像を交換しようとしましたが、私の解決策にはメモリ リークがあるようです:

System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
{
    // Pick a new bitmap
    static int resource = IDB_BITMAP1;
    if( resource == IDB_BITMAP2)
    {
        resource = IDB_BITMAP1;
    }
    else
    {
        resource = IDB_BITMAP2;
    }

    // Get the primary module
    Module^ mod = Assembly::GetExecutingAssembly()->GetModules()[0];

    // Get the instance handle 
    IntPtr hinst = Marshal::GetHINSTANCE(mod);

    // Get the bitmap as unmanaged
    HANDLE hbi = LoadImage((HINSTANCE) hinst.ToPointer(),MAKEINTRESOURCE(resource),IMAGE_BITMAP,0,0,LR_DEFAULTCOLOR); 

    // Import the unmanaged bitmap into the managed side 
    Bitmap^ bi = Bitmap::FromHbitmap(IntPtr(hbi));

    // Remove any previously stored images
    if(m_pictureBox1->Image != nullptr)
    {
        delete m_pictureBox1->Image;
        m_pictureBox1->Image = nullptr;
    }

    // Insert the bitmap into the picture box
    m_pictureBox1->Image = bi;

    // Free up the unmanaged bitmap
    DeleteObject(hbi);
}

私が見る限り、明示的にメモリを解放しているのに、ボタンがクリックされるたびにタスク マネージャがメモリの最大 24k の増加を報告するのはなぜですか?

4

2 に答える 2

1

2 つの単語: ガベージ コレクション

于 2009-10-14T12:56:37.917 に答える