On Thursday 31 October 2002 12:25 pm, Lars Gullik Bjønnes wrote:
> | I see that nobody has commented on my zombies patch.
> | Shall I just apply it?
>
> why not...

Ok. But before I do, I think that the current code leaks. I've got this
right too, haven't I? (See below).

Angus


// generate child in background
int Forkedcall::generateChild()
{
        const int MAX_ARGV = 255;
        char *syscmd = 0;
        char *argv[MAX_ARGV];

        string childcommand(command_); // copy
        bool more = true;
        string rest = split(command_, childcommand, ' ');

        int  index = 0;
        while (more) {
                childcommand = ltrim(childcommand);
                if (syscmd == 0) {
                        syscmd = new char[childcommand.length() + 1];
                        childcommand.copy(syscmd, childcommand.length());
                        syscmd[childcommand.length()] = '\0';
                }
                if (!childcommand.empty()) {
                        char * tmp = new char[childcommand.length() + 1];
                        childcommand.copy(tmp, childcommand.length());
                        tmp[childcommand.length()] = '\0';
                        argv[index++] = tmp;
                }

                // reinit
                more = !rest.empty();
                if (more)
                        rest = split(rest, childcommand, ' ');
        }
        argv[index] = 0;

#ifndef __EMX__
        pid_t cpid = ::fork();
        if (cpid == 0) { // child
                execvp(syscmd, argv);
                // If something goes wrong, we end up here
                string args;
                int i = 0;
                while (argv[i] != 0)
                        args += string(" ") + argv[i++];
                lyxerr << "execvp of \"" << syscmd << args << "\" failed: "
                       << strerror(errno) << endl;
+               _exit(1);
        }
#else
        pid_t cpid = spawnvp(P_SESSION|P_DEFAULT|P_MINIMIZE|P_BACKGROUND,
                             syscmd, argv);
#endif

        if (cpid < 0) { // error
                lyxerr << "Could not fork: "
                       << strerror(errno) << endl;
        }

+       // Clean-up.
+       delete [] syscmd;
+       for (int i = 0; i < MAX_ARGV; ++i) {
+               if (argv[i] == 0)
+                       break;
+               delete [] argv[i];
+       }
+
        return cpid;
}

Reply via email to