As the title states, i'm having issues on IOS with RenderTarget2D. I've appended reproduction code which resembles my original code. I'm unsure but It seems like an internal race condition and it happens about ~75% of the times on random moments. (which is typical for a race condition).
It's solvable - as the comment states - by not using RenderTarget2D to set the original data to... Use Texture2D instead. Still it seems like a tiny issue which perhaps can be fixed?
Thanks!
What version of MonoGame does the bug occur on:
- MonoGame 3.7 and latest development build (3.8.0.142).
What operating system are you using:
What MonoGame platform are you using:
The Exception.
Loaded assembly: /private/var/containers/Bundle/Application/995BA5D5-0486-49AC-85F5-CDAF78DD3BB7/<ApplicationID>.app/Xamarin.iOS.dll [External]
Loaded assembly: /private/var/containers/Bundle/Application/995BA5D5-0486-49AC-85F5-CDAF78DD3BB7/<ApplicationID>.app/System.dll [External]
Thread started: #2
Loaded assembly: /private/var/containers/Bundle/Application/995BA5D5-0486-49AC-85F5-CDAF78DD3BB7/<ApplicationID>.app/System.Core.dll [External]
Loaded assembly: /private/var/containers/Bundle/Application/995BA5D5-0486-49AC-85F5-CDAF78DD3BB7/<ApplicationID>.app/MonoGame.Framework.dll [External]
Loaded assembly: /private/var/containers/Bundle/Application/995BA5D5-0486-49AC-85F5-CDAF78DD3BB7/<ApplicationID>.app/<ApplicationID>.exe
2018-11-20 19:38:36.923 <ApplicationID>[1641:5916198] +[CATransaction synchronize] called within transaction
2018-11-20 19:38:36.923 <ApplicationID>[1641:5916198] +[CATransaction synchronize] called within transaction
2018-11-20 19:38:37.405 <ApplicationID>[1641:5916198] Done: 30 of total: 1000
2018-11-20 19:38:37.569 <ApplicationID>[1641:5916198] Done: 60 of total: 1000
2018-11-20 19:38:37.744 <ApplicationID>[1641:5916198] Done: 90 of total: 1000
2018-11-20 19:38:37.963 <ApplicationID>[1641:5916198] Done: 120 of total: 1000
2018-11-20 19:38:38.084 <ApplicationID>[1641:5916231]
Unhandled Exception:
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.Collections.Generic.Dictionary`2+KeyCollection+Enumerator[TKey,TValue].MoveNext () [0x00013] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.11.0.331/src/Xamarin.iOS/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/Dictionary.cs:1236
at Microsoft.Xna.Framework.Graphics.GraphicsDevice.PlatformDeleteRenderTarget (Microsoft.Xna.Framework.Graphics.IRenderTarget renderTarget) [0x000bc] in <1e6706e52d434020a9d7b7aa11ea4580>:0
at Microsoft.Xna.Framework.Graphics.RenderTarget2D.<Dispose>b__45_0 () [0x00006] in <1e6706e52d434020a9d7b7aa11ea4580>:0
at Microsoft.Xna.Framework.Threading.BlockOnUIThread (System.Action action) [0x00043] in <1e6706e52d434020a9d7b7aa11ea4580>:0
at Microsoft.Xna.Framework.Graphics.RenderTarget2D.Dispose (System.Boolean disposing) [0x00010] in <1e6706e52d434020a9d7b7aa11ea4580>:0
at Microsoft.Xna.Framework.Graphics.GraphicsResource.Finalize () [0x00000] in <1e6706e52d434020a9d7b7aa11ea4580>:0
2018-11-20 19:38:38.086 <ApplicationID>[1641:5916231] Unhandled managed exception:
Collection was modified; enumeration operation may not execute. (System.InvalidOperationException)
at System.Collections.Generic.Dictionary`2+KeyCollection+Enumerator[TKey,TValue].MoveNext () [0x00013] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.11.0.331/src/Xamarin.iOS/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/Dictionary.cs:1236
at Microsoft.Xna.Framework.Graphics.GraphicsDevice.PlatformDeleteRenderTarget (Microsoft.Xna.Framework.Graphics.IRenderTarget renderTarget) [0x000bc] in <1e6706e52d434020a9d7b7aa11ea4580>:0
at Microsoft.Xna.Framework.Graphics.RenderTarget2D.<Dispose>b__45_0 () [0x00006] in <1e6706e52d434020a9d7b7aa11ea4580>:0
at Microsoft.Xna.Framework.Threading.BlockOnUIThread (System.Action action) [0x00043] in <1e6706e52d434020a9d7b7aa11ea4580>:0
at Microsoft.Xna.Framework.Graphics.RenderTarget2D.Dispose (System.Boolean disposing) [0x00010] in <1e6706e52d434020a9d7b7aa11ea4580>:0
at Microsoft.Xna.Framework.Graphics.GraphicsResource.Finalize () [0x00000] in <1e6706e52d434020a9d7b7aa11ea4580>:0
2018-11-20 19:38:38.087 <ApplicationID>[1641:5916231] critical: Stacktrace:
2018-11-20 19:38:38.087 <ApplicationID>[1641:5916231] critical:
Native stacktrace:
2018-11-20 19:38:38.089 <ApplicationID>[1641:5916231] critical: 0 <ApplicationID> 0x0000000100f9fc34 <ApplicationID> + 8043572
2018-11-20 19:38:38.089 <ApplicationID>[1641:5916231] critical: 1 libsystem_platform.dylib 0x0000000181abcb58 _sigtramp + 52
2018-11-20 19:38:38.090 <ApplicationID>[1641:5916231] critical: 2 libsystem_pthread.dylib 0x0000000181ac2288 <redacted> + 376
2018-11-20 19:38:38.090 <ApplicationID>[1641:5916231] critical: 3 libsystem_c.dylib 0x000000018188fd0c abort + 140
2018-11-20 19:38:38.091 <ApplicationID>[1641:5916231] critical: 4 <ApplicationID> 0x00000001010ac0cc xamarin_get_block_descriptor + 8276
2018-11-20 19:38:38.092 <ApplicationID>[1641:5916231] critical: 5 <ApplicationID> 0x0000000100fdefe4 mono_pmip + 221688
2018-11-20 19:38:38.092 <ApplicationID>[1641:5916231] critical: 6 <ApplicationID> 0x00000001010437fc mono_pmip + 633360
2018-11-20 19:38:38.093 <ApplicationID>[1641:5916231] critical: 7 <ApplicationID> 0x0000000100fdfeb0 mono_pmip + 225476
2018-11-20 19:38:38.093 <ApplicationID>[1641:5916231] critical: 8 <ApplicationID> 0x00000001010570ec mono_pmip + 713472
2018-11-20 19:38:38.093 <ApplicationID>[1641:5916231] critical: 9 <ApplicationID> 0x0000000100fe0dec mono_pmip + 229376
2018-11-20 19:38:38.094 <ApplicationID>[1641:5916231] critical: 10 <ApplicationID> 0x0000000101043e5c mono_pmip + 634992
2018-11-20 19:38:38.094 <ApplicationID>[1641:5916231] critical: 11 libsystem_pthread.dylib 0x0000000181ac1220 <redacted> + 272
2018-11-20 19:38:38.095 <ApplicationID>[1641:5916231] critical: 12 libsystem_pthread.dylib 0x0000000181ac1110 <redacted> + 0
2018-11-20 19:38:38.095 <ApplicationID>[1641:5916231] critical: 13 libsystem_pthread.dylib 0x0000000181abfb10 thread_start + 4
2018-11-20 19:38:38.095 <ApplicationID>[1641:5916231] critical:
Here is a repro
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace ThreadTest.iOS {
public class Game1 : Game {
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Color[] cdata;
List<RenderTarget2D> tlist;
int dwidth = 500;
int dheight = 500;
int swidth = 0;
int sheight = 0;
int dsize;
float downscale = 0.5f;
int amount = 1000;
public Game1() {
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.IsFullScreen = true;
tlist = new List<RenderTarget2D>();
}
protected override void Initialize() {
base.Initialize();
dsize = dwidth * dheight;
swidth = (int)(dwidth * downscale);
sheight = (int)(dheight * downscale);
cdata = new Color[dsize];
Color color = Color.White;
for (int i = 0; i < dsize; i++) {
cdata[i] = color;
}
}
private bool render(int amount) {
// IF you set t to Texture2D on both places then no problems! Other platforms then IOS have no problems with this!
RenderTarget2D t;
RenderTarget2D r;
if (tlist.Count < this.amount) {
for (int i = 0; i < amount; i++) {
t = new RenderTarget2D(GraphicsDevice, dwidth, dheight);
t.SetData(cdata);
r = new RenderTarget2D(GraphicsDevice, swidth, sheight);
GraphicsDevice.SetRenderTarget(r);
GraphicsDevice.Clear(Color.Black * 0f);
spriteBatch.Begin();
spriteBatch.Draw(t, new Rectangle(0, 0, r.Width, r.Height), new Rectangle(0, 0, t.Width, t.Height), Color.White, 0, new Vector2(0, 0), SpriteEffects.None, 0);
spriteBatch.End();
GraphicsDevice.SetRenderTarget(null);
tlist.Add(r);
if (tlist.Count >= this.amount) {
return true;
}
}
}
return tlist.Count >= this.amount;
}
protected override void LoadContent() {
spriteBatch = new SpriteBatch(GraphicsDevice);
}
protected override void Update(GameTime gameTime) {
if (!render(30)) {
// NOT DONE YET, Wait, or show progress indicator
} else {
// DONE!.. Start game!
}
Console.WriteLine("Done: " + tlist.Count + " of total: " + amount);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime) {
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
}
}
}
As the title states, i'm having issues on IOS with RenderTarget2D. I've appended reproduction code which resembles my original code. I'm unsure but It seems like an internal race condition and it happens about ~75% of the times on random moments. (which is typical for a race condition).
It's solvable - as the comment states - by not using RenderTarget2D to set the original data to... Use Texture2D instead. Still it seems like a tiny issue which perhaps can be fixed?
Thanks!
What version of MonoGame does the bug occur on:
What operating system are you using:
What MonoGame platform are you using:
The Exception.
Here is a repro