home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!wupost!waikato.ac.nz!comp.vuw.ac.nz!actrix!Bruce.Hoult
- Newsgroups: comp.lang.c++
- Subject: Re: How to avoid calling destructor twice after fork()?
- Message-ID: <1992Jul30.120150.1507@actrix.gen.nz>
- From: Bruce.Hoult@bbs.actrix.gen.nz
- Date: Thu, 30 Jul 1992 12:01:50 GMT
- Sender: Bruce.Hoult@actrix.gen.nz (Bruce Hoult)
- References: <1992Jul29.180655.4716@cis.ohio-state.edu>
- Organization: Actrix Information Exchange
- Lines: 115
-
- In article <1992Jul29.180655.4716@cis.ohio-state.edu> gyu@cis.ohio-state.edu (george yu) writes:
- > I am using fork() system call in my C++ program. The problem is that each
- > calling of fork() creates a copy of calling process. The problem is any
- > instances created and not destroyed before fork() calling will be
- > destroyed twice. The following figure shows the problem.
- >
- > class MY_CLASS;
- > main()
- > {
- > MY_CLASS my_class;
- >
- > if(fork()==0)
- > {
- > // process 1
- > }
- > else
- > {
- > // process 2
- > }
- >
- > // everything beyond this point will be executed twice, including
- > // the destructor of my_class.
- > }
- >
- > Any suggestions?
-
-
- What's the problem? After the fork you've got two seperate objects in
- different processes and running the destructor on both of them is the
- correct thing to do -- the only possible problem is that the fork()
- call effectively does a default operator= to create the second copy
- instead of using any operator= that you've defined.
-
- If you really do have a problem with this then either ensure that the
- object is destroyed before the fork() ...
-
- {
- MY_CLASS my_class;
- // use it
- }
- if(fork()==0){
- // ...
-
- ... or else don't declare it until you get into the true and/or false
- branches of the fork().
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- --
- Bruce.Hoult@bbs.actrix.gen.nz Twisted pair: +64 4 477 2116
- BIX: brucehoult Last Resort: PO Box 4145 Wellington, NZ
- "Cray's producing a 200 MIPS personal computer with 64MB RAM and a 1 GB
- hard disk that fits in your pocket!" "Great! Is it PC compatable?"
-