britzl / gooey

Defold GUI system
MIT License
150 stars 22 forks source link

Bug: If dynamic list becomes empty, items have invalid data attached #59

Closed magroader closed 4 years ago

magroader commented 4 years ago

Because of this early bail:

    -- bail early if the list is empty
    if list.data_size == 0 then
        if refresh_fn then refresh_fn(list) end
        return list
    end

...

    update_dynamic_listitem_data(list)

When the list had items but then became empty, item.data for items that previously had data stays what it was before.

britzl commented 4 years ago

Thank you for the bug report! What is the best and easiest way to reproduce the problem?

magroader commented 4 years ago

Here is my basic setup:

    local list = gooey.dynamic_list(list_id, list_stencil, list_item_template, items)
    for i,item in ipairs(list.items) do
        if item.data then
            -- populate my gui nodes with data from item.data
        end
    end

If items has anything in it during one call to this, then later items has everything removed from it, then you will still get into the inner if statement, but with stale data that used to be, but is no longer, in the items list.

The gui does get disabled in this case, so the stale data isn't actually visible. However, the inner check does happen, so I need to actually detect that the data is stale (and not do anything that might crash).

The only reason I even noticed this was because I had a list of guid keys that I was using, and I was populating the gui based on data retrieved from a separate system keyed off those guids. The data retrieval returned nil (because the data had been removed), I tried to pass nil to a gui.set_text() call, and it crashed. I eventually traced it back to gooey giving me the stale data despite that data NOT being in the items list I passed.

I worked around it by doing this:

    if #items == 0 then
        -- Bug in gooey if no items - it was keeping the item.data property around but invalid
        -- https://github.com/britzl/gooey/issues/59
        for i=1,#list.items do
            list.items[i].data = nil
        end     
    end

However, after submitting this bug, I found this ALSO happens when items are removed from an overly full list - I had like 10 things in my list, and I deleted a few, and during one of these deletions I once again hit a case where items.data was stale. I suspect the overall list could hold N items, and when dropping down to N-1 pieces of data, I started to get stale data that wasn't in the array passed.

britzl commented 4 years ago

Thanks, I'll look into it during the week.