[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: Crash when using writeStdin()
From:       aleXXX <alexander.neundorf () gmx ! net>
Date:       2002-04-23 21:40:39
[Download RAW message or body]

On Tuesday 23 April 2002 18:32, Zenith Lau wrote:
> I want to write a kde program to communicate with a shellscript, and I use
> KProcess to run the shellscript. However, the program always crashes when
> it try to write to stdout. How can I solve this problem?
>
>
> ********************************************************
> writer.cpp
> ********************************************************
> Writer::Writer(){
> 	proc = new KProcess();
> 	*proc<<"/root/test";
> 	connect(proc, SIGNAL(receivedStdout(KProcess*, char*, int)),
>   	 	 this, SLOT(recv(KProcess*, char*, int)));
> 	connect(proc, SIGNAL(wroteStdin(KProcess*)), this,
> 		 SLOT(closeSlot(KProcess*)));
> 	proc->start(KProcess::Block, (KProcess::Communication)(KProcess::Stdin |
>             KProcess::Stdout));
> }

Are you sure you want to start it blocking ?
I didn't have a look at the header right now, but this means probably that 
proc->start() returns when the process has already exited.
Maybe you then still get the receivedStdout() signal and try to do something 
with the "dead" process, not sure, didn't test or have a look at the code.

> void Writer::recv(KProcess *p, char *buffer, int buflen){
> 	output = QString(buffer);
> 	if (output.find("hello")!=-1)
> 		sendoutput("abc");
> 	if (output.find("hi")!=-1)
> 		sendoutput("123");
> }
>
> void Writer::closeSlot(KProcess* p){
> 	proc->closeStdin();
> }
>
> void Writer::sendoutput(const char *s){
> 	proc->writeStdin(s, strlen(s));
> }

After reading "hello" and writing "abc" you will receive the wroteStdin() 
signal, and your closeSlot() will close the stdin, so when you receive "hi" 
and then try to write to stdin this will be already closed.
But usually this shouldn't crash your app.

Bye
Alex
 
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic