how to call the destructor in the case of a endless loop?

C

cdrsir

I have written a program like followings:

#include <iostream.h>
#include <fstream.h>

class cClass
{
ofstream *fout;
char ch;

public:
void pesudoTask() {
cin>>ch;
*fout<<ch<<endl;;
cout<<"have inputed: "<<ch<<endl;;
}
cClass() {
fout = new ofstream("mylog.txt");
*fout<<"program started"<<endl;
}
~cClass() {
*fout<<"program ended"<<endl;
fout->close();
}
};

int main(int argc, char *argv[])
{
cClass o;
while (1) o.pesudoTask();
return 0;
}

in this case, the program can be closed only by either press "Ctrl+c"
or directly click "x" in the shell windows, but the destructor is never
called, the "program ended" is not in the log file, how to do this?
 
M

Markus Schoder

cdrsir said:
I have written a program like followings:

#include <iostream.h>
#include <fstream.h>

Not standard headers better use

#include <iostream>
#include <fstream>

using namespace std;
class cClass
{
ofstream *fout;
char ch;

public:
void pesudoTask() {
cin>>ch;
*fout<<ch<<endl;;
cout<<"have inputed: "<<ch<<endl;;
}
cClass() {
fout = new ofstream("mylog.txt");
*fout<<"program started"<<endl;
}
~cClass() {
*fout<<"program ended"<<endl;
fout->close();

You need

delete fout;

here (which implicitliy closes the file as well) or your class will
leak memory.
}
};

int main(int argc, char *argv[])
{
cClass o;
while (1) o.pesudoTask();
return 0;
}

in this case, the program can be closed only by either press "Ctrl+c"
or directly click "x" in the shell windows, but the destructor is never
called, the "program ended" is not in the log file, how to do this?

This involves platform dependent behaviour. C++ knows about signals
however it is platform dependent when a given signal is delivered or if
at all.

Ctrl+C on some platforms is associated with the SIGINT signal which you
would need to handle to prevent your program from being terminated
without getting a chance to clean up after itself.

For example:

#include <iostream>
#include <fstream>
#include <csignal>

using namespace std;

namespace
{
sig_atomic_t exitRequest;

void handleSignal(int)
{
exitRequest = 1;
}
}

class cClass
{
ofstream *fout;
char ch;

public:
void pesudoTask() {
cin>>ch;
*fout<<ch<<endl;
cout<<"have inputed: "<<ch<<endl;
}
cClass() {
fout = new ofstream("mylog.txt");
*fout<<"program started"<<endl;
}
~cClass() {
*fout<<"program ended"<<endl;
delete fout;
}
};

int main(int argc, char *argv[])
{
signal(SIGINT, handleSignal);
cClass o;
while (!exitRequest) o.pesudoTask();
return 0;
}

Note that if a SIGINT is received the program will only exit after new
input has been received. Getting around this limitation is highly
platform dependent. If you are devloping for a POSIX platform you may
look for restarting system calls and the sigaction function however
this is off-topic for this group.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

I have written a program like followings:

#include <iostream.h>
#include <fstream.h>

class cClass
{
ofstream *fout;
char ch;

public:
void pesudoTask() {
cin>>ch;
*fout<<ch<<endl;;
cout<<"have inputed: "<<ch<<endl;;
}
cClass() {
fout = new ofstream("mylog.txt");
*fout<<"program started"<<endl;
}
~cClass() {
*fout<<"program ended"<<endl;
fout->close();
}
};

int main(int argc, char *argv[])
{
cClass o;
while (1) o.pesudoTask();
return 0;
}

in this case, the program can be closed only by either press "Ctrl+c"
or directly click "x" in the shell windows, but the destructor is never
called, the "program ended" is not in the log file, how to do this?

Since there is no way for the program to exit the loop (and thus the
only way to terminate the program is forcibly) there is no way for the
control-flow to reach the destructor and it will never be called. In
these cases it's not necessary to have one (tough it's good practice to
always have one).

Your greatest problem is to make sure that the output is really written
before the program is terminated (and not lost in a buffer), but since
you use endl you have solved this problem too.

Erik Wikström
 
J

John Carson

cdrsir said:
I have written a program like followings:

#include <iostream.h>
#include <fstream.h>

class cClass
{
ofstream *fout;
char ch;

public:
void pesudoTask() {
cin>>ch;
*fout<<ch<<endl;;
cout<<"have inputed: "<<ch<<endl;;
}
cClass() {
fout = new ofstream("mylog.txt");
*fout<<"program started"<<endl;
}
~cClass() {
*fout<<"program ended"<<endl;
fout->close();
}
};

int main(int argc, char *argv[])
{
cClass o;
while (1) o.pesudoTask();
return 0;
}

in this case, the program can be closed only by either press "Ctrl+c"
or directly click "x" in the shell windows, but the destructor is
never called, the "program ended" is not in the log file, how to do
this?


Don't write programs that get stuck in infinite loops.
 
A

Alan Johnson

John said:
cdrsir said:
I have written a program like followings:

#include <iostream.h>
#include <fstream.h>

class cClass
{
ofstream *fout;
char ch;

public:
void pesudoTask() {
cin>>ch;
*fout<<ch<<endl;;
cout<<"have inputed: "<<ch<<endl;;
}
cClass() {
fout = new ofstream("mylog.txt");
*fout<<"program started"<<endl;
}
~cClass() {
*fout<<"program ended"<<endl;
fout->close();
}
};

int main(int argc, char *argv[])
{
cClass o;
while (1) o.pesudoTask();
return 0;
}

in this case, the program can be closed only by either press "Ctrl+c"
or directly click "x" in the shell windows, but the destructor is
never called, the "program ended" is not in the log file, how to do
this?


Don't write programs that get stuck in infinite loops.

Somebody should make a compiler that issues a warning for programs that
will never halt.

(Yes, I know.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top