[smuxi] Engine: fixed leaking SQLite file descriptors



commit 4e5f4c573fdd417cf1e431d04e473939dd68c5b5
Author: Mirco Bauer <meebey meebey net>
Date:   Tue Aug 19 19:19:09 2014 +0200

    Engine: fixed leaking SQLite file descriptors
    
    SqliteCommand and SqliteDateReader have to be explicitly disposed else they leak
    file descriptors even when the database connection gets closed. The next GC run
    should in theory clean them up though.

 src/Engine/MessageBuffers/SqliteMessageBuffer.cs |   35 ++++++++++++----------
 1 files changed, 19 insertions(+), 16 deletions(-)
---
diff --git a/src/Engine/MessageBuffers/SqliteMessageBuffer.cs 
b/src/Engine/MessageBuffers/SqliteMessageBuffer.cs
index 2cfe2aa..36b483e 100644
--- a/src/Engine/MessageBuffers/SqliteMessageBuffer.cs
+++ b/src/Engine/MessageBuffers/SqliteMessageBuffer.cs
@@ -39,9 +39,10 @@ namespace Smuxi.Engine
 
         public override int Count {
             get {
-                var cmd = Connection.CreateCommand();
-                cmd.CommandText = "SELECT COUNT(*) FROM Messages";
-                return (int) Convert.ChangeType(cmd.ExecuteScalar(), typeof(int));
+                using (var cmd = Connection.CreateCommand()) {
+                    cmd.CommandText = "SELECT COUNT(*) FROM Messages";
+                    return (int) Convert.ChangeType(cmd.ExecuteScalar(), typeof(int));
+                }
             }
         }
 
@@ -136,15 +137,16 @@ namespace Smuxi.Engine
                 param.Value = limit.ToString();
                 cmd.Parameters.Add(param);
 
-                var reader = cmd.ExecuteReader();
-                var msgs = new List<MessageModel>(limit);
-                while (reader.Read()) {
-                    var json = (string) reader["JSON"];
-                    var dto = JsonSerializer.DeserializeFromString<MessageDtoModelV1>(json);
-                    var msg = dto.ToMessage();
-                    msgs.Add(msg);
+                using (var reader = cmd.ExecuteReader()) {
+                    var msgs = new List<MessageModel>(limit);
+                    while (reader.Read()) {
+                        var json = (string) reader["JSON"];
+                        var dto = JsonSerializer.DeserializeFromString<MessageDtoModelV1>(json);
+                        var msg = dto.ToMessage();
+                        msgs.Add(msg);
+                    }
+                    return msgs;
                 }
-                return msgs;
             }
         }
 
@@ -171,11 +173,12 @@ namespace Smuxi.Engine
             using (var cmd = Connection.CreateCommand()) {
                 cmd.CommandText = "SELECT JSON FROM Messages";
 
-                var reader = cmd.ExecuteReader();
-                while (reader.Read()) {
-                    var json = (string) reader["JSON"];
-                    var dto = JsonSerializer.DeserializeFromString<MessageDtoModelV1>(json);
-                    yield return dto.ToMessage();
+                using (var reader = cmd.ExecuteReader()) {
+                    while (reader.Read()) {
+                        var json = (string) reader["JSON"];
+                        var dto = JsonSerializer.DeserializeFromString<MessageDtoModelV1>(json);
+                        yield return dto.ToMessage();
+                    }
                 }
             }
         }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]