[RESOLVED] Is it safe to modify an argument from in a slot function?



  • 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?


  • Moderators

    Hi,

    [quote]Now, I thought that this argument passed in is a copy that I can “do what I want with”[/quote]That is correct. You defined your slot to take parameters by-value after all -- the only way to do that is by copying.

    Your crash is caused by something else. As a first step, run your program in a debugger and see its stack trace.



  • Thanks for that, this resolves my question! :)

    Found the bug a few lines later :o


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.