为什么即使是非常简单的应用程序,MonoTouch 也会导致大量内存泄漏(根据 Instruments 的报告)?

发布于 2024-10-17 14:32:43 字数 6954 浏览 2 评论 0原文

我正在使用 Instruments 运行一个非常基本的测试应用程序,它发现了很多内存泄漏!因为我知道 Apple 人员在将应用程序提交到 iTunes 时会检查内存泄漏,所以我想调查一下这个问题。

我的环境:Mac OS X 10.6.6 上的 MonoDevelop 2.4.2 和 MonoTouch 3.2.4,针对运行 iOS 4.2.1 的 iPad。

我的测试应用程序只是显示一个 TableView,其中填充了 50 个字符串的列表,并按起始字母对它们进行分组。

重现问题的步骤:使用 MonoDevelop 创建一个新的“基于 iPad 窗口的项目”,使用 Interface Builder 打开 MainWindow.xib 文件,将新的 TableView 放置到窗口上,并为 AppDelegate 类创建其出口(名为“tview”) 。 然后在Main.cs中输入以下代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using MonoTouch.Foundation;
    using MonoTouch.UIKit;

    namespace SimpleTable
    {
        public class Application
        {
            static void Main (string[] args)
            {
                UIApplication.Main (args);
            }
        }

        public partial class AppDelegate : UIApplicationDelegate
        {
            private List<string> _names;

            public override bool FinishedLaunching (UIApplication app, NSDictionary options)
            {
                _names = new List<string> { "Smith", "Jones", "Williams", "Brown", "Taylor",
                                            "Davies", "Wilson", "Evans", "Thomas", "Johnson",
                                            "Roberts", "Walker", "Wright", "Robinson", "Thompson",
                                            "White", "Hughes", "Edwards", "Green", "Hall",
                                            "Wood", "Harris", "Lewis", "Martin", "Jackson",
                                            "Clarke", "Clark", "Turner", "Hill", "Scott",
                                            "Cooper", "Morris", "Ward", "Moore", "King",
                                            "Watson", "Baker", "Harrison", "Morgan", "Patel",
                                            "Young", "Allen", "Mitchell", "James", "Anderson",
                                            "Phillips", "Lee", "Bell", "Parker", "Davis" };
                tview.Source = new MyTableViewSource(_names);

                window.MakeKeyAndVisible ();

                return true;
            }

            private class MyTableViewSource : UITableViewSource
            {
                private List<string> _sectionTitles;
                private SortedDictionary<int, List<string>> _sectionElements = new SortedDictionary<int, List<string>>();

                public MyTableViewSource(List<string> list)
                {
                    // Use LINQ to find the distinct set of alphabet characters required.
                    _sectionTitles = (from c in list select c.Substring(0, 1)).Distinct().ToList();

                    // Sort the list alphabetically.
                    _sectionTitles.Sort();

                    // Add each element to the List<string> according to the letter it starts with
                    // in the SortedDictionary<int, List<string>>.
                    foreach (string element in list)
                    {
                        int sectionNum = _sectionTitles.IndexOf(element.Substring(0, 1));
                        if (_sectionElements.ContainsKey(sectionNum)) 
                        {
                            // SortedDictionary already contains a List<string> for that letter.
                            _sectionElements[sectionNum].Add(element);
                        } 
                        else
                        {
                            // First time that letter has appeared, create new List<string> in the SortedDictionary.
                            _sectionElements.Add(sectionNum, new List<string> { element });
                        }
                    }
                }

                public override int NumberOfSections(UITableView tableView)
                {
                    return _sectionTitles.Count;
                }

                public override string TitleForHeader(UITableView tableView, int section)
                {
                    return _sectionTitles[section];
                }

                public override int RowsInSection(UITableView tableview, int section)
                {
                    return _sectionElements[section].Count;
                }

                public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
                {
                    string kCellIdentifier = "mycell";
                    UITableViewCell cell = tableView.DequeueReusableCell(kCellIdentifier);
                    if (cell == null)
                    {
                        // No re-usable cell found, create a new one.
                        cell = new UITableViewCell(UITableViewCellStyle.Default, kCellIdentifier);
                    }

                    string display = _sectionElements[indexPath.Section][indexPath.Row];
                    cell.TextLabel.Text = display;

                    return cell;
                }

                public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
                {
                    string display = _sectionElements[indexPath.Section][indexPath.Row];

                    showAlert("RowSelected", "You selected: \"" + display + "\"");

                    // Prevent the blue 'selection indicator' remaining.
                    tableView.DeselectRow(indexPath, true);
                }

                private void showAlert(string title, string message)
                {
                    using (var alert = new UIAlertView(title, message, null, "OK", null))
                    {
                        alert.Show();
                    }
                }
            }
        }
    }

我在设备上进行了以下测试:

  1. 注释掉了

    public override string TitleForHeader(UITableView tableView, int section)
    

    程序,从 Instruments 内启动应用程序:发现单个泄漏;看来这个泄漏始终存在,即使在测试空应用程序时也是如此!

    测试 1 仪器屏幕截图

  2. 未注释

    public override string TitleForHeader(UITableView tableView, int section)
    

    程序,从 Instruments 内启动应用程序:发现大量泄漏,并且当上下滚动表格和/或选择任何行时,泄漏的数量会增加。

    测试 2 台仪器屏幕截图

  3. 替换为

    return _sectionTitles[节];
    

    声明

    public override string TitleForHeader(UITableView tableView, int section)
    

    程序

    return“测试标头...”;
    

    (因此使用常量字符串):与测试 n.2 相同!

MonoTouch 是否有问题或者我忘记了一些重要的事情?如果即使是这样一个简单的应用程序在运行几分钟时也会产生数百次内存泄漏,那么真正的(并且更复杂的)应用程序会发生什么情况?

我在网上进行了广泛的搜索,但没有找到任何关于这个问题的重要帖子......任何贡献将不胜感激。

I'm running a very basic test App with Instruments, and it finds a lot of memory leaks! Since I know that Apple guys perform checks for leaked memory when an App is submitted to iTunes, I would like to investigate the problem.

My environment: MonoDevelop 2.4.2 with MonoTouch 3.2.4 on Mac OS X 10.6.6, targeting an iPad running iOS 4.2.1.

My test App simply shows a TableView populated with a list of 50 strings, grouping them by their starting letter.

Steps to reproduce the problem: create a new "iPad Window-based Project" with MonoDevelop, open the MainWindow.xib file with Interface Builder, place a new TableView onto the window and create its outlet (named "tview") to the AppDelegate class.
Then enter the following code in Main.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using MonoTouch.Foundation;
    using MonoTouch.UIKit;

    namespace SimpleTable
    {
        public class Application
        {
            static void Main (string[] args)
            {
                UIApplication.Main (args);
            }
        }

        public partial class AppDelegate : UIApplicationDelegate
        {
            private List<string> _names;

            public override bool FinishedLaunching (UIApplication app, NSDictionary options)
            {
                _names = new List<string> { "Smith", "Jones", "Williams", "Brown", "Taylor",
                                            "Davies", "Wilson", "Evans", "Thomas", "Johnson",
                                            "Roberts", "Walker", "Wright", "Robinson", "Thompson",
                                            "White", "Hughes", "Edwards", "Green", "Hall",
                                            "Wood", "Harris", "Lewis", "Martin", "Jackson",
                                            "Clarke", "Clark", "Turner", "Hill", "Scott",
                                            "Cooper", "Morris", "Ward", "Moore", "King",
                                            "Watson", "Baker", "Harrison", "Morgan", "Patel",
                                            "Young", "Allen", "Mitchell", "James", "Anderson",
                                            "Phillips", "Lee", "Bell", "Parker", "Davis" };
                tview.Source = new MyTableViewSource(_names);

                window.MakeKeyAndVisible ();

                return true;
            }

            private class MyTableViewSource : UITableViewSource
            {
                private List<string> _sectionTitles;
                private SortedDictionary<int, List<string>> _sectionElements = new SortedDictionary<int, List<string>>();

                public MyTableViewSource(List<string> list)
                {
                    // Use LINQ to find the distinct set of alphabet characters required.
                    _sectionTitles = (from c in list select c.Substring(0, 1)).Distinct().ToList();

                    // Sort the list alphabetically.
                    _sectionTitles.Sort();

                    // Add each element to the List<string> according to the letter it starts with
                    // in the SortedDictionary<int, List<string>>.
                    foreach (string element in list)
                    {
                        int sectionNum = _sectionTitles.IndexOf(element.Substring(0, 1));
                        if (_sectionElements.ContainsKey(sectionNum)) 
                        {
                            // SortedDictionary already contains a List<string> for that letter.
                            _sectionElements[sectionNum].Add(element);
                        } 
                        else
                        {
                            // First time that letter has appeared, create new List<string> in the SortedDictionary.
                            _sectionElements.Add(sectionNum, new List<string> { element });
                        }
                    }
                }

                public override int NumberOfSections(UITableView tableView)
                {
                    return _sectionTitles.Count;
                }

                public override string TitleForHeader(UITableView tableView, int section)
                {
                    return _sectionTitles[section];
                }

                public override int RowsInSection(UITableView tableview, int section)
                {
                    return _sectionElements[section].Count;
                }

                public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
                {
                    string kCellIdentifier = "mycell";
                    UITableViewCell cell = tableView.DequeueReusableCell(kCellIdentifier);
                    if (cell == null)
                    {
                        // No re-usable cell found, create a new one.
                        cell = new UITableViewCell(UITableViewCellStyle.Default, kCellIdentifier);
                    }

                    string display = _sectionElements[indexPath.Section][indexPath.Row];
                    cell.TextLabel.Text = display;

                    return cell;
                }

                public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
                {
                    string display = _sectionElements[indexPath.Section][indexPath.Row];

                    showAlert("RowSelected", "You selected: \"" + display + "\"");

                    // Prevent the blue 'selection indicator' remaining.
                    tableView.DeselectRow(indexPath, true);
                }

                private void showAlert(string title, string message)
                {
                    using (var alert = new UIAlertView(title, message, null, "OK", null))
                    {
                        alert.Show();
                    }
                }
            }
        }
    }

I have performed the following tests on the device:

  1. commented out

    public override string TitleForHeader(UITableView tableView, int section)
    

    procedure, launched App from within Instruments: a single leak is found; it seems that this leak is ALWAYS present, even when testing an empty App!

    Test 1 Instruments screenshot

  2. uncommented

    public override string TitleForHeader(UITableView tableView, int section)
    

    procedure, launched App from within Instruments: a lot of leaks are found, and their number grows up when scrolling the table up and down and/or selecting any row.

    Test 2 Instruments screenshot

  3. substituted the

    return _sectionTitles[section];
    

    statement in

    public override string TitleForHeader(UITableView tableView, int section)
    

    procedure with

    return "Test Header…";
    

    (thus using a constant string): the same as in test n.2!

Is MonoTouch bugged or am I forgetting something essential? If even such a simple App generates hundreds of memory leaks when running for a few minutes, what could happen with a real (and more complex) App?

I have searched the web extensively but I haven't found any significant post about this problem... any contribution will be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梅倚清风 2024-10-24 14:32:43

仪器并不完全理解 monotouch 和 mono vm 分配和管理内存的方式。苹果并未拒绝任何相关申请。如果您认为存在泄漏的特定错误,请在 monotouch bugtracker 中提交问题,网址为 http://monotouch.net/支持

话虽这么说,我查看了您的屏幕截图,确实发现了可能发生的 1 个 16 字节泄漏,以及您的第二个屏幕截图中的泄漏,并针对即将推出的 monotouch 4 修复了该问题。

The way monotouch and the mono vm allocates and manages memory isn't fully understood by instruments. Apple has not rejected any applications for this. If you have a specific bug that you think is a leak please file an issue in the monotouch bugtracker at http://monotouch.net/Support

That being said, I looked at your screenshots and did find 1 16-byte leak that could happen, and the leak in your second screenshot, and fixed that for the upcoming monotouch 4.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文