Bug 1446

Summary: Mono.Debugger.Soft.ObjectCollectedException: The requested operation cannot be completed because the object has been garbage collected.
Product: [Mono] Runtime Reporter: Marek Safar <masafa>
Component: DebuggerAssignee: Zoltan Varga <vargaz>
Severity: major CC: mkrueger, mono-bugs+monodevelop, mono-bugs+mono, norfolc+bugzilla
Priority: Normal    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Tags: Is this bug a regression?: ---
Last known good build:

Description Marek Safar 2011-10-12 07:18:59 UTC
MD master + Mono master

using System;
using System.Threading;
using System.Threading.Tasks;

public class TestPostContext
	static Task[]      tasks;
	static readonly int max = 24;
	static void InitWithDelegate (Action action)
		tasks = new Task[max];
		for (int i = 0; i < max; i++) {
			tasks [i] = Task.Factory.StartNew (action);
	public static int Main ()
		int counter = 0;
		InitWithDelegate (delegate {
			throw new ApplicationException (counter.ToString ()); });

		try {
			Task.WaitAll (tasks);
		} catch (AggregateException e) {
			Console.WriteLine (e.InnerExceptions);	// Set breakpoint here and hover over e.InnerExceptions then expand Items
		return 0;
Comment 1 Alexander Riman 2011-10-23 04:59:02 UTC
This error can occur when doing something like this using MonoTouch:

We have the next code:
string s = "Hello, World!";
byte[] b = System.Text.Encoding.UTF8.GetBytes(s);
return; // Set breakpoint here
Place breakpoint on the line with return,
open Expression Evaluator and enter the next value in it:


We will get as a result "Hello, World!".
But it is not possible to open text using plus sign (this is needed to copy value to the clipboard). If we do this we will get the error "Mono.Debugger.Soft.ObjectCollectedException: The requested operation cannot be completed because the object has been garbage collected."
Comment 2 Jeffrey Stedfast 2011-12-05 15:16:15 UTC
I guess what needs to happen here is that the runtime needs to keep objects alive for the duration of the method rather than allowing any variables to be collected before the method exits.
Comment 3 Mikayla Hutchinson [MSFT] 2011-12-05 15:48:15 UTC
AFAIK the .NET debugger extends variable liveness to the end of the method so users can inspect the values more easily. We should probably do the same.
Comment 4 Zoltan Varga 2011-12-13 01:19:25 UTC
The problem here is that the debuggee keeps a weak reference on the objects it returns to the client, in order to not influence program behaviour, and the GC is not disabled while the debuggee is stopped. InnerExceptions returns a transient object:

				return innerExceptions.AsReadOnly ();

which is not kept alive by anything, so it can be freed any time.
Comment 5 Zoltan Varga 2011-12-13 01:41:46 UTC
The jdi docs says:

Note that while the target VM is suspended, no garbage collection will occur because all threads are suspended. The typical examination of variables, fields, and arrays during the suspension is safe without explicitly disabling garbage collection.

This is not really true in mono, because there are things done by the debugger agent which can trigger a gc, like the creation of weak references.
Comment 6 Zoltan Varga 2012-12-21 23:38:57 UTC
Fixed in master/mobile-master.