admin管理员组

文章数量:1193758

I am trying to learn Vala by writing some app. I faced a memory leak problem which I cannot identify and fix. The app basically iterates through the files in a given directory at a specified interval. Then it executes the executable files from that directory one by one, catching their output and then writing it to a named pipe. If a subprocess takes too long to execute, it cancels it.

I think the problem lies somewhere in the following code snippet (subprocessmunicate_utf8_async ?):

private async bool execute(string file_path, uint timeout_sec, DataOutputStream channel) {
        print("Executing %s\n", file_path);

        try {
            int status = 0;
            string output = null;
            string errors = null;
            Cancellable cancellable = new Cancellable();

            Subprocess subprocess = new Subprocess(SubprocessFlags.STDOUT_PIPE | SubprocessFlags.STDERR_PIPE, file_path);

            uint timeout_id = Timeout.add_seconds(timeout_sec, () => {
                    print("Cancelling subprocess...\n");
                    cancellable.cancel();
                    subprocess.force_exit();
                    return Source.REMOVE;
                }, Priority.LOW);

            try {
                yield subprocessmunicate_utf8_async(null, cancellable, out output, out errors);

                status = subprocess.get_exit_status();

                if (subprocess.get_successful() && output != null) {
                    print("OUT:\n%s", output);
                    string[] lines = output.split("\n" );
                    write_to_channel(channel, lines);
                } else {
                    stderr.printf("Subprocess returned code %d with errors: %s\n", status, errors);
                    return false;
                }

                return true;

            } catch (IOError.CANCELLED e) {
                stderr.printf("Subprocess was cancelled.\n");
            } finally {
                if (!cancellable.is_cancelled()) {
                    Source.remove(timeout_id);
                }
                subprocess.dispose();
            }

        } catch (Error e) {
            stderr.printf("Subprocess execution error: %s\n", e.message);
        }
        return false;
    }

Does anyone see what is wrong here? Also, any pieces of advice with regards to the idiomatic way of doing such things in Vala would be much appreciated

本文标签: Memory leak when using Subprocess in ValaStack Overflow