9

Generally speaking, what is the correct way to deep copy Qt containers? I'm not worried about deep copying the containers recursively, although addressing such would be helpful.

Freedom_Ben
  • 11,247
  • 10
  • 69
  • 89
  • 2
    The above code works fine for me - why do you think it won't? – cmannett85 May 28 '13 at 19:53
  • @cmannett85 I have run it through GDB and seen that both maps contain "value2" – Freedom_Ben May 28 '13 at 19:55
  • Yes, I misread the code at first, this snippet should work fine. Deleting my answer. – Matt Phillips May 28 '13 at 20:00
  • 1
    Once again, not for me - and I'm also using Qt 4.8 (4.8.4 x64 Linux to be precise). Can you post the full code you are using to test this? `QMap` is implicitly shared, but as soon as you modify it, the COW mechanism will kick in and a deep copy will occur (disregarding `QString`'s implicit sharing of course). – cmannett85 May 28 '13 at 20:00
  • Works fine also with Qt 5.1 in Linux. –  May 28 '13 at 20:17
  • @cmannett85 Yes I can post the full code. I'll need to sanitize a couple of things and then I'll post. Thanks – Freedom_Ben May 28 '13 at 20:28
  • How about adding `Q_ASSERT(_savedMap[modelToSave] != _modifiedMap[modelToSave]);` between first and second line (not counting comment lines). Just to check that you are really making some changes to the map. –  May 28 '13 at 21:15
  • @Roku I added the Q_ASSERT you suggested and sure enough it is failing. Now the quest moves on it that direction. I'll update the question when I'm finished for future travelers. – Freedom_Ben May 30 '13 at 19:04
  • @cmannett85 I found the issue and it was not with Qt. Nevertheless I think the question (revised to remove non-applicable code) may be helpful to others in the future. Do you want to post an answer based on your comment? If so, I'll accept it. Thanks for your help. – Freedom_Ben May 30 '13 at 21:49

1 Answers1

20

Despite what everyone will tell you - that you don't deep copy Qt containers - there are situations in which you simply need to perform an actual deep copy instead of just a shallow one. To do that, use detach():

container1 = container2;
container1.detach();
kralyk
  • 4,249
  • 1
  • 32
  • 34
  • 1
    Awesome. This is the answer – Freedom_Ben Jul 03 '14 at 00:49
  • Can you give an example of when you would need to do this? The moment you modify `container1` it would perform a deep copy, if you don't modify it - why would you want to copy it!? – cmannett85 Jul 03 '14 at 07:49
  • 3
    @cmannett85 Just yesterday I needed to do exactly that. I have two threads, one receives data over a channel (similar to socket), process the data and stores it in a couple of containers. The other thread renders the data by periodically checking for new data. If there are new data, it locks a mutex and copies data from some of the containers - this is where I need to deep-copy. If I didn't perform deep copy, locking would be done for each of the containers seperately when the data processing thread modifies them, which would degrade performance. – kralyk Jul 04 '14 at 09:52