Bug 40665 - Null reference in call to Marshal.PtrToStructure()
Summary: Null reference in call to Marshal.PtrToStructure()
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: 6.0.1 (C6SR1)
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
Depends on:
Reported: 2016-04-24 23:00 UTC by Luke Lanphear
Modified: 2016-11-28 08:33 UTC (History)
3 users (show)

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

Android project fails, Windows project works. (Same code) (1.70 MB, application/zip)
2016-04-24 23:00 UTC, Luke Lanphear
Example that stills generates this error on Cycle 9 (19.07 KB, application/x-zip-compressed)
2016-11-24 10:51 UTC, David Dunscombe

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 Developer Community or GitHub 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 Luke Lanphear 2016-04-24 23:00:11 UTC
Created attachment 15825 [details]
Android project fails, Windows project works. (Same code)

I am attempting to search a file without loading it into memory. When I try to call Marshal.PtrToStructure(), I am getting a null reference exception.

The same call works under windows. I have included a sample solution which demonstrates the problem.
Comment 1 Marek Habersack 2016-10-10 11:35:14 UTC
The issue no longer occurs with XA/master (to be released in Cycle 9). You can test the latest XA/master on Mac using this build: https://bosstoragemirror.blob.core.windows.net/wrench/monodroid-mavericks-master/cf/cf2beeaf61722a06763b2935ca84a81365c8e6b7/xamarin.android-7.0.99-121.pkg
Comment 2 David Dunscombe 2016-11-23 12:13:51 UTC
I'm still seeing this issue for various structs on cycle 9 release (Alpha) for both XS and VS.
Comment 3 David Dunscombe 2016-11-24 10:51:05 UTC
Created attachment 18644 [details]
Example that stills generates this error on Cycle 9

This shows the issue is still present in Cycle 9. It appears connected to the packing of the structs, as if i remove those it works.
Comment 4 David Dunscombe 2016-11-24 11:09:36 UTC
I have also seen some strange bugs, where the PtrToStructure succeeds and the values of the generated structure look OK in the debugger, but when i assign them to another variable or pass them to a function they get corrupted, e.g. a float in the struct may look like 354.6475 in the debugger, but when assigned to another float it would get 4.853315E+11. That related issue I've not managed to replicate in a separate project.
Comment 5 David Dunscombe 2016-11-28 08:33:26 UTC
Some extra info, this works ok on the simulator, fails on device (armabi-v7a)