[Qt-interest] a litte mild 'rant'

Andreas Pakulat apaku at gmx.de
Mon Mar 30 21:30:40 CEST 2009


On 30.03.09 18:31:39, wim.delvaux at adaptiveplanet.com wrote:
> > > > So having to free the memory yourself would be better?
> > >
> > > Well QString manages string memory space too. perhaps it can manage an
> >
> > 'ascii'
> >
> > > stringsspace too.
> >
> > It does.. that's why it is deleting it when it goes out of scope.
> 
> It should not 'delete' the ascii stringspace when it goes out of context but 
> is deleted.
> 
> E.g. when you have a QLineEdit() and call ->text() on it, surely the string 
> does not go out of scopy.  However
> 
> 	QLineEdit()->text().toLatin1().constData() is bad ...

You didn't read the api for QLineEdit::text(). It returns a _copy_ of the
internally saved text and that copied object exists only temporary until
the line has been executed completely. After that the temporary is being
deleted and hence takes all its data with it.

On top of that toLatin1() also returns a _copy_ of a latin1-encoded
bytearray of that string, which again goes out of scope at the end of the
line. And constData only returns an internal data from the bytearray, not
the string!

Andreas

-- 
After your lover has gone you will still have PEANUT BUTTER!



More information about the Qt-interest-old mailing list