Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [RESOLVED] Is it safe to modify an argument from in a slot function?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 779 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    code_fodder
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      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.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • C Offline
        C Offline
        code_fodder
        wrote on last edited by
        #3

        Thanks for that, this resolves my question! :)

        Found the bug a few lines later :o

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved