Bug 3840 - ListViewItem.Serialize doesn't check if Group is null
Summary: ListViewItem.Serialize doesn't check if Group is null
Alias: None
Product: Class Libraries
Classification: Mono
Component: Windows.Forms ()
Version: 2.10.x
Hardware: PC Windows
: Lowest normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2012-03-10 05:27 UTC by rick
Modified: 2012-09-02 15:12 UTC (History)
3 users (show)

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

The patch I've applied to my local build to correct the problem (612 bytes, patch)
2012-03-10 06:49 UTC, rick
Test case (9.12 KB, application/octet-stream)
2012-06-13 16:18 UTC, Thomas Goldstein
Test case (modified) (6.37 KB, application/zip)
2012-06-13 20:59 UTC, rick

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 rick 2012-03-10 05:27:55 UTC
Mono's Windows.Forms simply adds info for Group regardless if it's null or not, ie:

info.AddValue ("Group", group);

Microsoft.NET will crash if it encounters a null Group, since it expects it to not be null if present (tries to access .Name).
Comment 1 rick 2012-03-10 06:49:36 UTC
Created attachment 1493 [details]
The patch I've applied to my local build to correct the problem
Comment 2 Thomas Goldstein 2012-06-13 14:02:23 UTC
Could you please come up with a unit test, or at least a test case for this? I tried to come up with one, but I can't manage to serialize a ListViewItem. I get an exception that says it can't serialize the Font property, because it has no parameterless constructor.

Here's my code so far:

[Test] // Xamarin bug 3840
public void TestNullGroupSerialization ()
	ListViewItem item = new ListViewItem ();
	item.Group = null;
	XmlSerializer ser = new XmlSerializer(item.GetType()); // Does not work
	StringBuilder sb = new StringBuilder();
	StringWriter writer = new StringWriter(sb);
	ser.Serialize(writer, item);
Comment 3 rick 2012-06-13 14:33:39 UTC
I'm not sure if you could create an adequate test for this since the problem is Mono is serializing ListViewItem in a way that causes MS.NET to crash when deserializing when running a Mono compiled executable.

You can trigger the problem by simply creating a new WinForms project, adding a ListView, adding an item to that ListView, compiling that project under Mono, then run the resulting executable under MS.NET.
Comment 4 Thomas Goldstein 2012-06-13 16:18:16 UTC
Created attachment 2052 [details]
Test case

I followed your instructions, but it worked for me. I created the WinForms project with ListView and ListViewItem, built it on Linux with Mono (.NET 3.5 profile), in Debug and Release mode, then tried the executables on Windows + .NET, and they worked with no issues.

Here's my WinForms project.
Comment 5 rick 2012-06-13 20:59:11 UTC
Created attachment 2054 [details]
Test case (modified)

Ahh, sorry, I fixed this in my local build awhile ago that I forgot the exact details, so I reproduced the problem.

ListView.Items become serialized as part of resources (.resx) when the form is Localizable (so the strings in the ListView.Items are localizable).

Attached is the modified version of your test case, which reproduces the crash, the only thing I did was change Localizable on the form to True and save.
Comment 6 Thomas Goldstein 2012-06-14 14:20:42 UTC
Alright, I've pushed your patch (commit b9d24579836115a452ca148a8ee8e82f4085f03b). Thanks!