[Interest] QTextStream doesn't write to QFile

Igor Mironchik igor.mironchik at gmail.com
Fri Jan 16 13:01:13 CET 2015


Hi.

On Fri, 16 Jan 2015 14:44:08 +0300, Koehne Kai  
<Kai.Koehne at theqtcompany.com> wrote:

>
>
>> -----Original Message-----
>> From: interest-bounces+kai.koehne=theqtcompany.com at qt-project.org
>> [mailto:interest-bounces+kai.koehne=theqtcompany.com at qt-project.org]
>> On Behalf Of Igor Mironchik
>> Sent: Friday, January 16, 2015 12:24 PM
>> To: interest at qt-project.org
>> Subject: [Interest] QTextStream doesn't write to QFile
>>
>> Hi. I'm doing a simple log class. I'm sure that Log creates after
>> QCoreApplication.
>> QCoreApplication is static. I create QCoreApplication as:
>>
>> static QSharedPointer< QCoreApplication > application(
>> 	int argc = 0, char ** argv = 0 )
>> {
>> 	static QSharedPointer< QCoreApplication > app(
>> 		new QCoreApplication( argc, argv ) );
>>
>> 	return app;
>> }
>
> It seems you're desperately trying here to extend QCoreApplications  
> lifetime as long as possible. This is just asking for trouble: just let  
> QCoreApplication be destructed when your application exits, in main.cpp.

I'm not desperately trying to extens QCoreApplication lifetime. I need it  
for that reason that Log is singleton and uses QObjects. So Log have to be  
destructed before QCoreApplication.

It's normal practice when using singletons in Qt apps. It works well in  
other my application.

But in this test it crashes. However in another app I use GUI, so there  
QApplication instead.

>
>> But this Log doesn't write anything to the file. File creates, I'm sure  
>> in it. But
>> nothing writes. What is the reason?
>
> Log is again declared as a static object, so the destructor runs when  
> the app is already unloading: This might be just too late to write stuff  
> with QFile.
>
> Instead, I'd use qAddPostRoutine or QCoreApplication::aboutToQuit() to  
> close the log file ... or just flush the file after every access: This  
> involves of course some overhead, but if your app crashes before exiting  
> you have at least some logging data :)

You prompted the idea that I need flush() every time when writing  
something. For test I did it. And now application doesn't crashes at exit  
and writes to the log. Hmm. It's strange.



-- 
Best Regards,
Igor Mironchik.



More information about the Interest mailing list