Search code examples
c++listmemory-managementfreecoverity

Error: “Wrapper object use after free (WRAPPER_ESCAPE)”


...
Object& a = list.front();
list.pop_front();
...

I need to use &a after doing the above. But coverity is raising the use after free issue. Is using &a after doing pop_front() a problem?


Solution

  • With

    Object& a = list.front();
    

    you make a reference the front element of the list. When you later pop the front element, it is destructed leaving you with a reference to a non-existing object.

    You should copy the element instead

    Object a = list.front();
    

    Or possibly move it

    Object a = std::move(list.front());