Changeset 2230


Ignore:
Timestamp:
Jan 17, 2013, 11:19:30 PM (7 years ago)
Author:
sam
Message:

base: fix a nasty double free issue in Array::Remove().

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lol/base/array.h

    r2216 r2230  
    164164    void Remove(int pos, int todelete = 1)
    165165    {
    166         /* FIXME: we need to call dtors for the first
    167          * todelete elements here */
    168166        for (int i = pos; i + todelete < m_count; i++)
    169167            m_data[i] = m_data[i + todelete];
     
    183181        /* Not enough elements? Add some. */
    184182        for (int i = m_count; i < count; ++i)
    185             m_data[i] = e;
     183            new(&m_data[i]) Element(e);
    186184
    187185        m_count = count;
  • trunk/test/unit/array.cpp

    r2216 r2230  
    1818namespace lol
    1919{
     20
     21struct TrackedObj
     22{
     23    static int m_ctor, m_dtor;
     24
     25    TrackedObj() { m_ctor++; }
     26    TrackedObj(TrackedObj const &) { m_ctor++; }
     27    ~TrackedObj() { m_dtor++; }
     28};
     29
     30int TrackedObj::m_ctor = 0;
     31int TrackedObj::m_dtor = 0;
    2032
    2133LOLUNIT_FIXTURE(ArrayTest)
     
    120132        LOLUNIT_ASSERT_EQUAL(b[3], 3);
    121133    }
     134
     135    LOLUNIT_TEST(ElementCtorDtor)
     136    {
     137        /* Ensure array elements get created and destroyed the proper
     138         * number of times. */
     139        TrackedObj::m_ctor = 0;
     140        TrackedObj::m_dtor = 0;
     141        {
     142            Array<TrackedObj> a;
     143
     144            a.Push(TrackedObj());
     145        }
     146        LOLUNIT_ASSERT_EQUAL(TrackedObj::m_ctor, TrackedObj::m_dtor);
     147
     148        TrackedObj::m_ctor = 0;
     149        TrackedObj::m_dtor = 0;
     150        {
     151            Array<TrackedObj> a;
     152
     153            a.Resize(2);
     154            a.Resize(4);
     155            a.Resize(1);
     156        }
     157        LOLUNIT_ASSERT_EQUAL(TrackedObj::m_ctor, TrackedObj::m_dtor);
     158    }
    122159};
    123160
Note: See TracChangeset for help on using the changeset viewer.