This is Xamarin's bug tracking system. For product support, please use the support links listed in your Xamarin Account.
Bug 1446 - Mono.Debugger.Soft.ObjectCollectedException: The requested operation cannot be completed because the object has been garbage collected.
: Mono.Debugger.Soft.ObjectCollectedException: The requested operation cannot b...
Status: RESOLVED FIXED
Product: Runtime
Classification: Mono
Component: Debug
: unspecified
: PC Linux
: Normal major
: ---
Assigned To: Zoltan Varga
:
:
:
:
  Show dependency treegraph
 
Reported: 2011-10-12 07:18 EDT by Marek Safar
Modified: 2012-12-21 23:38 EST (History)
4 users (show)

See Also:
Tags:
Test Case URL:
External Submit: ---


Attachments

Description Marek Safar 2011-10-12 07:18:59 EDT
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 {
            counter++;
            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 EDT
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:

System.Text.Encoding.UTF8.GetString(b)

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 EST
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 Michael Hutchinson 2011-12-05 15:48:15 EST
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 EST
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 EST
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 EST
Fixed in master/mobile-master.

Note You need to log in before you can comment on or make changes to this bug.