Search code examples
c++stdvector

How to do error handling with vector of pointers?


This is probably a noob question, but I am a bit confused.

I have a block of code that looks like this:

std::vector<MyObject*> datas;
try
{
 MyObject data = get_me_data();
 MyObject* dat=&data;
 datas.push_back(dat);
}
catch (...) {}
do_something_with_datas(datas);

The issue is that function after try/catch code that should handle std::vector does not work properly as I am getting nonsense from the function.

I think the issue is that dat goes out of scope in try block, so state of what it points is undefined and I really shouldn't have used MyObject* dat=&data? I am also uncertain if vector is cleared properly.

Can someone please point me at right direction? I have some algorithm that creates bunch of MyObject data objects. I wish to pack pointers to them into a vector and process them after the try/catch code.

This is simplified code, actual algorithm uses MyObject* because the code implements iterative algorithm that updates dat object before pushing them into vector. So main issue here is how to make get_me_data() function give me a pointer to data, and not the data. Would I need to use new operator at the line when it creates the object?

I am new to C++, and I am certain there are many better ways to implement this, can someone explain how would move semantics be used here. What I understood from move constructors is that they turn rvalue into lvalue and provide a more efficient copy, i.e they'd move pointers that point to array fields and "neglect" original object, so the memory would be "moved" to a new object, instead of copied. I am not sure if that'd work with the code I wrote as the idea is to iteratively update fields of MyObject. As all classes in the code need MyObject as their input, it made little sense to me to move it around and ensuring that every class in the package is moveable. It seems much easier to have one MyObject and to pass a pointer to it across the whole package, and not be bothered too much about ownership. Am I horribly wrong with this implementation?


Solution

  • I think the issue is that dat goes out of scope in try block

    Your hunch is correct.

    I am also uncertain if vector is cleared properly.

    Although this cannot be determined, based on the shown code this is very likely. This happens very often in code that suffers from a common problem called "Pointless Use Of Pointers".

    There's nothing in the shown code that requires pointers. Maybe there might be some other reason, that's not shown, but as as the shown code is concerned there is no reason why the vector cannot simply be:

    std::vector<MyObject> datas;
    

    And the try/catch block gets reduced to:

    datas.push_back(get_me_data());
    

    It's unclear whether this still needs exception handling. Whether it does or does not everything now works correctly, either way. Finally: whether or not the "vector is cleared properly" is now a completely moot point. It will be automatically cleared properly. The std::vector will make sure that its contents gets correctly cleared when it is destroyed.

    I have some algorithm that creates bunch of MyObject data objects.

    Great, here they are, in their entirety: std::vector<MyObject>. Just say "No" to pointless use of pointers.