Linked Questions

Popular Questions

Possible Memory Issue with WinAPI ListView

Asked by At

I'm creating a SQL client with WinAPI. Every time a user submits a query, the current columns and items of my ListView are deleted, then new columns and items are created based on the results of the query.

I've noticed through Visual Studio heap profiler, that every time the columns and items are created, 3 objects and ~60 bytes are allocated, and I can't figure out why. Examining the snapshot suggests this has to do with adding SubItems via the ListView_SetItem function (_ListView_OnSetItem is the last thing I can identify in Stacks View).

The results I'm using to test with create 4 columns and 36 items/subitems.

To the best of my knowledge, I've implemented the ListView stuff correctly. Still, here are my two functions that reset and populate the ListView. Any help is appreciated.

void ResetListView(HWND hWnd) {

    HWND hWndHdr = (HWND)SendMessage(GetDlgItem(hWnd, IDQ_LISTVIEW), LVM_GETHEADER, 0, 0);
    int numColumns = (int)SendMessage(hWndHdr, HDM_GETITEMCOUNT, 0, 0L);

    ListView_DeleteAllItems(GetDlgItem(hWnd, IDQ_LISTVIEW));
    for (int i = 0; i < numColumns; i++)
        ListView_DeleteColumn(GetDlgItem(hWnd, IDQ_LISTVIEW), 0);

void CreateListView(HWND hWnd, QueryResults * queryResults) {

    LVCOLUMN lvc;
    ZeroMemory(&lvc, sizeof(LVCOLUMN));
    lvc.fmt = LVCFMT_CENTER;

    int numHeaders = queryResults->numHeaders;

    // Add columns
    for (int i = 0; i < numHeaders; i++) {
        lvc.iSubItem = i;
        lvc.pszText = queryResults->headers[i]; = 100;
        ListView_InsertColumn(GetDlgItem(hWnd, IDQ_LISTVIEW), i, (LPARAM)&lvc);

    LVITEM lvi;
    ZeroMemory(&lvi, sizeof(LVITEM));
    lvi.mask = LVIF_TEXT;

    // Add items and subitems
    for (int i = 0; i < queryResults->rows.size(); i++) {
        lvi.iItem = i;

        lvi.pszText = queryResults->rows[i]->cells[0];
        lvi.iSubItem = 0;
        ListView_InsertItem(GetDlgItem(hWnd, IDQ_LISTVIEW), (LPARAM)&lvi);

        for (int j = 1; j < numHeaders; j++) {
            lvi.pszText = queryResults->rows[i]->cells[j];
            lvi.iSubItem = j;
            ListView_SetItem(GetDlgItem(hWnd, IDQ_LISTVIEW), (LPARAM)&lvi);


Related Questions