简体   繁体   English

C ++ / CLI代码中的内存泄漏

[英]Memory leak in c++/CLI code

The test app: 测试应用:

static void Main(string[] args)
    {
        String connectionString = "DSN=DotNetUltraLightDSII; UID=user; PWD=pass";
        String query = "select * from person";

        using (OdbcConnection connection = new OdbcConnection(connectionString))
        {
            connection.Open();
            for (Int32 trial = 1; trial <= 100000; trial++)
            {
                if (0 == trial % 1000)
                {
                    Console.WriteLine("Executing trial #{0:#,0}", trial);
                    GC.Collect();
                }

                using (OdbcCommand command = new OdbcCommand(query, connection))
                {
                    using (OdbcDataReader reader = command.ExecuteReader())
                    {
                        do
                        {
                            while (reader.Read())
                            {
                                for (Int32 colIndex = 0; colIndex < reader.FieldCount; colIndex++)
                                {
                                    Object x = reader[colIndex];
                                    IDisposable d = x as IDisposable;
                                    if (null != d)
                                    {
                                        d.Dispose();
                                    }
                                    d = null;
                                    x = null;
                                }
                            }
                        } while (reader.NextResult());
                        reader.Close();
                    }
                }
            }

            connection.Close();
        }

Memory usage Heap Snapshot Comparison 内存使用情况 堆快照比较

The memory usage just goes up over time. 内存使用随时间增加。 It seems like there are strong handles to the ULPersonTable but I cannot tell what exactly these strong handles are or where they are coming from. 似乎ULPersonTable有强大的句柄,但我无法确定这些强大的句柄到底是什么或它们来自何处。 We are storing the instances of that type inside gcroot, but they seem to be destroyed. 我们将这种类型的实例存储在gcroot中,但它们似乎已被破坏。 If someone could shed some light on this issue, that would be appreciated. 如果有人可以阐明这个问题,将不胜感激。

Regards 问候

Ended up finding the problem. 最终发现问题。 It was a pointer which wasn't being deleted properly. 这是一个指针,未正确删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM