Domanda

Sono stato cercando di scambiare le immagini in un controllo PictureBox in un'applicazione C ++ / CLI, ma la mia soluzione sembra avere una perdita di memoria:

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);
}

Per quanto posso vedere, sto esplicitamente liberando la memoria così perché fa un task manager rapporto ~ 24K aumento in memoria ogni volta che il pulsante viene premuto?

È stato utile?

Soluzione

due parole: la raccolta dei rifiuti

Altri suggerimenti

Stranamente, questo in realtà sembra essere causato quando il mouse-over il pulsante. Ogni volta che fate che la memoria salta ma dopo abbastanza mouse-over che l'utilizzo della memoria si stabilizza. I clic reali sul tasto (vale a dire le chiamate alla mia routine) non causano perdite.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top