[mythtv] [PATCH] Playlist feature UI tweaking

Joseph A. Caputo jcaputo1 at comcast.net
Thu Jan 27 12:19:44 EST 2005


On Thursday 27 January 2005 10:48, Kevin Kuphal wrote:
> I think I found it.  In the randomize function it did a playList = 
> randomList which for QStringLists makes a reference copy of the list.  
> The randomPlay list is locally scoped and I think dies by the time you 
> get over to playSelected which means the pointers are all messed up 
and 
> hence the segfault.

Nope; wrong.  QStringList is one of Qt's "implicitly shared" classes, 
meaning that the internal storage is passed by reference, but the class 
maintains a reference count, so even though the local stack variable 
randomList goes out of scope, the underlying list data is preserved 
because Qt knows that playList is still referencing it.  You'll 
probably have to look deeper for the source of your segfault.

This mechanism is the main reason why all of Qt's implicitly shared 
classes should be passed *by value* when being used as the return value 
of a function (though passing by reference is fine as in input 
parameter).  So,

const QStringList & someFunction()

is bad; use

QStringList someFunction()

instead (not that this necessarily applies here, but it's a good thing 
to know about Qt).  The shared mechanism also means that there is not 
much overhead when passing by value (the size of the object is 
independent of the size of the underlying list data, and the sharing 
mechanism means copy takes constant time), so you wouldn't really gain 
much by passing by reference anyway.

The other thing to remember about Qt's implicit sharing mechanism is 
that it employs a copy-on-write/modify mechanism.  Meaning that:

playList = randomList

Causes both variables to share a single copy of the underlying data 
(with reference counts).  As soon as one of those variables is 
modified, however, it causes the modified object to make a copy of the 
data and detach from the shared copy (thus decrementing the reference 
count).

For more information on Qt's implicit sharing mechanism, see here:

http://doc.trolltech.com/3.3/shclass.html


-JAC


More information about the mythtv-dev mailing list