andyedinborough / aenetmail

C# POP/IMAP Mail Client
370 stars 153 forks source link

Memory eater #107

Open NimeCloud opened 11 years ago

NimeCloud commented 11 years ago

This code piece consumes the all memory in my 8GB PC.

            MailMessage msg = new MailMessage();
            msg.Load("x", false);
andyedinborough commented 11 years ago

I’m working on this right now actually. Can you make a pull request so that your changes are highlighted?

Andy

From: NimeCloud [mailto:notifications@github.com] Sent: Friday, November 2, 2012 8:32 AM To: andyedinborough/aenetmail Subject: Re: [aenetmail] Memory eater (#107)

ReadLine seems buggy and I modified it as below:

    //

    internal static string ReadLine(this Stream stream, ref int maxLength, Encoding encoding, char? termChar) {

    if (stream.CanTimeout)

        stream.ReadTimeout = 10000;

    var maxLengthSpecified = maxLength > 0;

    byte b = 0, b0;

    int current = 0; // <-- Added a counter

    using (var mem = new MemoryStream()) {

        while (true) {

            b0 = b;

            b = (byte)stream.ReadByte();

            if (maxLengthSpecified) maxLength--;

            if (maxLengthSpecified && mem.Length == 1 && b == termChar && b0 == termChar) {

                maxLength++;

                continue;

            }

            if (b == 10 || b == 13) {

                if (mem.Length == 0 && b == 10) {

                    continue;

                } else break;

            }

            mem.WriteByte(b);

            if (maxLengthSpecified && maxLength == 0)

                break;

            // Check the length

            current++;

            if (current >= mem.Length)

                break;

        }

        return encoding.GetString(mem.ToArray());

    }

}

— Reply to this email directly or view it on GitHubhttps://github.com/andyedinborough/aenetmail/issues/107#issuecomment-10014241.