Search code examples
c++qtsignals-slots

Is it safe to modify an argument from in a slot function in Qt?


I have a signal that emits a std::vector<uint8_t> which contains a load of data (a buffer).

Then in the receiving objects I have a slot that takes an argument std::vector<uint8_t> data

Then I try to decode this data. One of the things I have to do is remove padding, for example:

void receive_slot(std::vector<uint8_t> data)
{
    // Remove padding
    if (data.size() > 20)
        data.resize(data.size() - 20);
}

Now, I thought that this argument passed in is a copy that I can "do what I want with". However, doing a re-size of larger then ~10 bytes crashes my program. I assume that re-sizing less then ~10 bytes does not crash it only by luck.

I therefore assume that I can't safely do this and should first copy it to a new data buffer.

Can anyone enlighten me regarding this?


Solution

  • To provide a direct anwser to the question:

    Slots in Qt are just normal function calls (when and with which arguments they're called is the difference, managed by Qt), and it's perfectly valid to modify function arguments (when non-const obviously). You're right when you say that you're given a copy and that you can do "do what you want with it".

    In this case, the error doesn't purely come from the fact that you're modifying the function argument.