Skip to content

WeakReference causes stack corruption when null #614

Closed
nanoframework/metadata-processor
#104
@AdrianSoundy

Description

@AdrianSoundy

Details about Problem

Target:
Tested on ESP32 but probably all targets

Firmware image version:
Latest - 1.7.2-preview.7

Description

Tests with Graphic code.

When a weakReference is assigned a null everything after breaks or firmware restarts.

In the original code a look up was made on a hashtable and when object not present a null is returned into a weakReference, when method returned containing this assignment it had impossible value for result.

I have provided a simple test code to highlight problem.

  • With commented out code in WeakTest() enabled it normally crashes on return
  • Without commented out code it returns OK but myobj value is 0
public class Program
{
	public static void Main()
	{
		Thread.Sleep(3000);  // allow erase when crashed
		try
		{
			object myobj = WeakTest();
			if (myobj != null)
				Console.WriteLine("myobj not null");
			else
				Console.WriteLine("myobj is null");
		}
		catch (Exception) { };

		Thread.Sleep(Timeout.Infinite);
	}

	public static object WeakTest()
	{
		object wobj = null;
		WeakReference wref = (WeakReference)wobj;
//		if (wref != null)
//			Console.WriteLine("not null");
//		else
//			Console.WriteLine("is null");

		return wobj;
	}
}

Detailed repro steps so we can see the same problem

  1. Single step until return from WorkTest() method
  2. It will crash or obj has value of 0

Other suggested things

Expected behaviour

When WeakReference is assigned a null it should just be a null and not screw up stack

Make an effort to fix the bug

Had a look at code but not sure where to look.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions