Bug 1446 - Mono.Debugger.Soft.ObjectCollectedException: The requested operation cannot be completed because the object has been garbage collected.
Summary: Mono.Debugger.Soft.ObjectCollectedException: The requested operation cannot b...
Alias: None
Product: Runtime
Classification: Mono
Component: Debugger ()
Version: unspecified
Hardware: PC Linux
: Normal major
Target Milestone: ---
Assignee: Zoltan Varga
Depends on:
Reported: 2011-10-12 07:18 UTC by Marek Safar
Modified: 2012-12-21 23:38 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

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.