QIODevice::write and QByteArray
-
I want to write a QByteArray (which stores PDF if this is relevant) to QFile with this code:
@QByteArray filledForm = form->getFilledForm( formsToFill.second );
if( filledForm.isEmpty() )
{
qDebug() << "data is empty";
return;
}
QFile formFile( fileName );
if( !formFile.open( QIODevice::WriteOnly ) )
{
qDebug() << "Unable to open";
qDebug() << formFile.errorString();
return;
}
if( formFile.write( filledForm ) < 0 )
{
qDebug() << "Unable to write";
qDebug() << formFile.errorString();
return;
}@End every time i get
@Unable to write
The supplied user buffer is not valid for the requested operation.@This error remains on another PC. What can cause it?
-
It's a windows error message (would have been handy if you provided us some info about your environment in the first place, btw).
It's a "common error":http://lmgtfy.com/?q=The+supplied+user+buffer+is+not+valid+for+the+requested+operation which unfortunately nobody has a general solution for.
What's the exact filepath your're trying to write?
Does it work with another path? -
Sorry, next time i'll provide info about environment in the main post.
Windows 7, Qt 4.7.4, MSVS2008.[quote author="Volker" date="1317205153"]It's a "common error":http://lmgtfy.com/?q=The+supplied+user+buffer+is+not+valid+for+the+requested+operation which unfortunately nobody has a general solution for.[/quote]
Yes, i know, i've been searching for a few hours for solution. It's now more interesting not to know just the solution, but what in general can cause this error.
I'm trying to write right in the Desktop folder. Each time i try an empty file is created. Other paths don't work either.
-
[quote author="p-himik" date="1317206485"]
[quote author="Volker" date="1317205153"]It's a "common error":http://lmgtfy.com/?q=The+supplied+user+buffer+is+not+valid+for+the+requested+operation which unfortunately nobody has a general solution for.[/quote]Yes, i know, i've been searching for a few hours for solution. It's now more interesting not to know just the solution, but what in general can cause this error.
[/quote]It would be helpful, if you had mentioned your search efforts beforehand. We cannot know - the crystal ball is still defective and even Google didn't tell us ;-)
[quote author="p-himik" date="1317206485"]I'm trying to write right in the Desktop folder. Each time i try an empty file is created. Other paths don't work either.
[/quote]- Is it with the byte array write only or can you open/write using other methods?
- Did you try to write a simple helloworld.txt?
- Is a virus scanner running, which could intercept the write?
- Can you create a file using wordpad.exe or another editor (Qt Creator)?
- Does your byte array contain any bytes at all?
-
[quote author="Volker" date="1317247933"]It would be helpful, if you had mentioned your search efforts beforehand. We cannot know - the crystal ball is still defective and even Google didn't tell us ;-)[/quote]
In case you need people to test crystal ball on here i am ;)
Just wrote
@QByteArray filledForm( "test" );@
instead of the fist line of the code provided in the main post. And it worked, i got file containing "test".
How can contents of QByteArray (and it's not empty - second line of the code) prevent writing this QByteArray to a file? I thought that QByteArray is just, as says it's name, an array of bytes - nothing more (considering only data of course).Also i forgot to mention that code in the main post works on Linux flawlessly.
-
It could be due to a virus scanner or other things.
-
Unfortunately disabling virus scanner gave nothing.
Oh... well, i just did a stupid thing - just to kill some time i guess. Every time QFile::write returned this error i chopped of 1kB from QByteArray and tried again to write it. At some point (let's say 'B') program crashed. And again. And again. And unexpectedly it wrote this file (at the same point 'B'). I increased chop() argument that it will chop almost to 'B' and relaunched app again. And now it crashes at QByteArray::chop. I completely confused. How the hell can QByteArray::chop() crash my app? Documentation says: "If n is greater than size(), the result is an empty byte array.". More than that, wrapping chop() with try-catch gave nothing - code in catch is never executed.
-
That would be funny if it wasn't so sad. I returned all code back to it's first state (just like in the main post) and replaced 14'th line with this:
@if( formFile.write( filledForm.data(), filledForm.size() ) < 0 )@
And app crashed just at this line! The actual culprit is filledForm.data().Ok, let's dig into data()'s source code.
-
Hm...
@inline const char *QByteArray::data() const
{ return d->data; }
inline int QByteArray::size() const
{ return d->size; }@
The first one crashes my app while the second doesn't. Am i missing something? As i can see this code can crash only and only if 'd' is null. But why than the second function is working? -
A-ha! There is another one data() which calls detach(). And actually the detach() is crashing.
-
I'm stupid. And i'm very sorry for wasting your time.
In the form->getFilledForm( formsToFill.second ); i used QByteArray::fromRawData. With stack objects.
And now i'm wondering what made me write QByteArray::fromRawData instead of just a QByteArray's constructor.
And why did it work on Linux?