<master> Пример утечки памяти при обращеннии к свойству SuperView

This commit is contained in:
Rustam Zaitov 2014-03-13 13:26:13 +04:00
parent 1d0bcce5c6
commit 2aeb0c29b4
9 changed files with 343 additions and 0 deletions

View File

@ -0,0 +1,31 @@
<Properties>
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug|iPhoneSimulator" PreferredExecutionTarget="MonoDevelop.IPhone.IPhoneSimulatorTarget.IPhoneRetina4.7.0" />
<MonoDevelop.Ide.Workbench ActiveDocument="RefCycles/MainController.cs">
<Files>
<File FileName="RefCycles/AppDelegate.cs" Line="1" Column="1" />
<File FileName="RefCycles/MainController.cs" Line="56" Column="17" />
</Files>
<Pads>
<Pad Id="ProjectPad">
<State expanded="True">
<Node name="RefCycles" expanded="True">
<Node name="MainController.cs" selected="True" />
</Node>
</State>
</Pad>
<Pad Id="ClassPad">
<State expanded="True" selected="True" />
</Pad>
<Pad Id="MonoDevelop.Debugger.WatchPad">
<State />
</Pad>
</Pads>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore />
</MonoDevelop.Ide.DebuggingService.Breakpoints>
<MonoDevelop.Ide.DebuggingService.PinnedWatches />
<MonoDevelop.Ide.ItemProperties.RefCycles>
<ProfileHeapShotSettings_27CDebug Output="/var/folders/zp/6tv_g63d2qn78sgtswk126640000gn/T/profiler-output.mlpd" Zip="True" HeapshotMode="ondemand" HeapshotGCInterval="1" HeapshotTimeInterval="1000" Calls="False" Alloc="False" CallDepth="100" StatisticalSampling="False" SampingFrequency="1000" MaxFrames="8" Mode="Memory" />
</MonoDevelop.Ide.ItemProperties.RefCycles>
</Properties>

View File

@ -0,0 +1,32 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SuperViewMemLeak", "SuperViewMemLeak\SuperViewMemLeak.csproj", "{5C615257-AE59-40B4-9BDC-1F614526BEAD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|iPhoneSimulator = Debug|iPhoneSimulator
Release|iPhoneSimulator = Release|iPhoneSimulator
Debug|iPhone = Debug|iPhone
Release|iPhone = Release|iPhone
Ad-Hoc|iPhone = Ad-Hoc|iPhone
AppStore|iPhone = AppStore|iPhone
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5C615257-AE59-40B4-9BDC-1F614526BEAD}.Ad-Hoc|iPhone.ActiveCfg = Ad-Hoc|iPhone
{5C615257-AE59-40B4-9BDC-1F614526BEAD}.Ad-Hoc|iPhone.Build.0 = Ad-Hoc|iPhone
{5C615257-AE59-40B4-9BDC-1F614526BEAD}.AppStore|iPhone.ActiveCfg = AppStore|iPhone
{5C615257-AE59-40B4-9BDC-1F614526BEAD}.AppStore|iPhone.Build.0 = AppStore|iPhone
{5C615257-AE59-40B4-9BDC-1F614526BEAD}.Debug|iPhone.ActiveCfg = Debug|iPhone
{5C615257-AE59-40B4-9BDC-1F614526BEAD}.Debug|iPhone.Build.0 = Debug|iPhone
{5C615257-AE59-40B4-9BDC-1F614526BEAD}.Debug|iPhoneSimulator.ActiveCfg = Debug|iPhoneSimulator
{5C615257-AE59-40B4-9BDC-1F614526BEAD}.Debug|iPhoneSimulator.Build.0 = Debug|iPhoneSimulator
{5C615257-AE59-40B4-9BDC-1F614526BEAD}.Release|iPhone.ActiveCfg = Release|iPhone
{5C615257-AE59-40B4-9BDC-1F614526BEAD}.Release|iPhone.Build.0 = Release|iPhone
{5C615257-AE59-40B4-9BDC-1F614526BEAD}.Release|iPhoneSimulator.ActiveCfg = Release|iPhoneSimulator
{5C615257-AE59-40B4-9BDC-1F614526BEAD}.Release|iPhoneSimulator.Build.0 = Release|iPhoneSimulator
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = SuperViewMemLeak\SuperViewMemLeak.csproj
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,22 @@
<Properties>
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug|iPhoneSimulator" PreferredExecutionTarget="MonoDevelop.IPhone.IPhoneSimulatorTarget.IPhoneRetina4.7.0" />
<MonoDevelop.Ide.Workbench>
<Pads>
<Pad Id="ProjectPad">
<State expanded="True">
<Node name="SuperViewMemLeak" expanded="True" selected="True" />
</State>
</Pad>
<Pad Id="ClassPad">
<State expanded="True" selected="True" />
</Pad>
<Pad Id="MonoDevelop.Debugger.WatchPad">
<State />
</Pad>
</Pads>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore />
</MonoDevelop.Ide.DebuggingService.Breakpoints>
<MonoDevelop.Ide.DebuggingService.PinnedWatches />
</Properties>

View File

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace RefCycles
{
// The UIApplicationDelegate for the application. This class is responsible for launching the
// User Interface of the application, as well as listening (and optionally responding) to
// application events from iOS.
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
// class-level declarations
UIWindow window;
MainController _mainController;
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
window = new UIWindow (UIScreen.MainScreen.Bounds);
_mainController = new MainController ();
window.RootViewController = _mainController;
window.MakeKeyAndVisible ();
return true;
}
}
}

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
</dict>
</plist>

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDisplayName</key>
<string>RefCycles</string>
<key>CFBundleIdentifier</key>
<string>com.your-company.RefCycles</string>
<key>UIDeviceFamily</key>
<array>
<integer>1</integer>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>MinimumOSVersion</key>
<string>6.0</string>
</dict>
</plist>

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace RefCycles
{
public class Application
{
// This is the main entry point of the application.
static void Main (string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main (args, null, "AppDelegate");
}
}
}

View File

@ -0,0 +1,77 @@
using System;
using MonoTouch.UIKit;
using System.Drawing;
using System.Reflection;
namespace RefCycles
{
public class MainController : UIViewController
{
private UIButton _button;
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
View.BackgroundColor = UIColor.White;
_button = new UIButton (new RectangleF(10f, 30f, 0f, 0f));
_button.TouchUpInside += HandleTouchUpInside;
_button.SetTitle ("click me", UIControlState.Normal);
_button.SetTitleColor (UIColor.Blue, UIControlState.Normal);
_button.SizeToFit ();
View.AddSubview (_button);
}
private void HandleTouchUpInside (object sender, EventArgs e)
{
Console.WriteLine ("HandleTouchUpInside");
/*
// Dont leak
UIView v1 = new UIView ();
UIView v2 = new UIView ();
*/
/*
// Dont leak
UIView v1 = new DebugView ();
UIView v2 = new UIView ();
*/
/*
// Leak
UIView v1 = new UIView ();
UIView v2 = new DebugView ();
*/
// Leak
UIView v1 = new DebugView ();
UIView v2 = new DebugView ();
v1.AddSubview (v2);
View.AddSubview (v1);
var sv = v2.Superview; // cause of leak
//v2.ResetSuperViewChache (); // uncomment to fix
v1.RemoveFromSuperview ();
}
}
public class DebugView: UIView
{
}
public static class Ext
{
public static void ResetSuperViewChache(this UIView view)
{
Type t = typeof(UIView);
FieldInfo fInfo = t.GetField("__mt_Superview_var", BindingFlags.NonPublic | BindingFlags.Instance);
fInfo.SetValue (view, null);
}
}
}

View File

@ -0,0 +1,105 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">iPhoneSimulator</Platform>
<ProductVersion>10.0.0</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{5C615257-AE59-40B4-9BDC-1F614526BEAD}</ProjectGuid>
<ProjectTypeGuids>{6BC8ED88-2882-458C-8E55-DFD12B67127B};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<OutputType>Exe</OutputType>
<RootNamespace>RefCycles</RootNamespace>
<IPhoneResourcePrefix>Resources</IPhoneResourcePrefix>
<AssemblyName>RefCycles</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|iPhoneSimulator' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\iPhoneSimulator\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<MtouchLink>None</MtouchLink>
<CodesignEntitlements>Entitlements.plist</CodesignEntitlements>
<MtouchDebug>true</MtouchDebug>
<MtouchProfiling>true</MtouchProfiling>
<MtouchUseSGen>true</MtouchUseSGen>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|iPhoneSimulator' ">
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\iPhoneSimulator\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<MtouchLink>None</MtouchLink>
<ConsolePause>false</ConsolePause>
<CodesignEntitlements>Entitlements.plist</CodesignEntitlements>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|iPhone' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\iPhone\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<CodesignEntitlements>Entitlements.plist</CodesignEntitlements>
<MtouchDebug>true</MtouchDebug>
<CodesignKey>iPhone Developer</CodesignKey>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|iPhone' ">
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\iPhone\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodesignEntitlements>Entitlements.plist</CodesignEntitlements>
<ConsolePause>false</ConsolePause>
<CodesignKey>iPhone Developer</CodesignKey>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Ad-Hoc|iPhone' ">
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\iPhone\Ad-Hoc</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<CodesignEntitlements>Entitlements.plist</CodesignEntitlements>
<BuildIpa>true</BuildIpa>
<CodesignProvision>Automatic:AdHoc</CodesignProvision>
<CodesignKey>iPhone Distribution</CodesignKey>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'AppStore|iPhone' ">
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\iPhone\AppStore</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodesignKey>iPhone Distribution</CodesignKey>
<CodesignEntitlements>Entitlements.plist</CodesignEntitlements>
<ConsolePause>false</ConsolePause>
<CodesignProvision>Automatic:AppStore</CodesignProvision>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Xml" />
<Reference Include="System.Core" />
<Reference Include="monotouch" />
</ItemGroup>
<ItemGroup>
<Folder Include="Resources\" />
</ItemGroup>
<ItemGroup>
<None Include="Info.plist" />
<None Include="Entitlements.plist" />
</ItemGroup>
<ItemGroup>
<Compile Include="Main.cs" />
<Compile Include="AppDelegate.cs" />
<Compile Include="MainController.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>