Bug 34509 - F# - Xamarin.Mac Document App - Failed to marshal the Objective-C object (type: __NSStackBlock__)
Summary: F# - Xamarin.Mac Document App - Failed to marshal the Objective-C object (typ...
Status: RESOLVED DUPLICATE of bug 30927
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Library (Xamarin.Mac.dll) ()
Version: 2.4.0 (C6)
Hardware: Macintosh Mac OS
: High normal
Target Milestone: 2.8.0 (C7)
Assignee: Chris Hamons
URL:
Depends on:
Blocks:
 
Reported: 2015-10-01 17:43 UTC by Frank A. Krueger
Modified: 2015-12-09 09:28 UTC (History)
3 users (show)

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


Attachments
Minimum Repro (358.53 KB, application/zip)
2015-10-06 11:00 UTC, Frank A. Krueger
Details


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:
Status:
RESOLVED DUPLICATE of bug 30927

Description Frank A. Krueger 2015-10-01 17:43:53 UTC
When performing the Duplicate command in a document based app, Xamarin throws the following error:

System.Exception: Failed to marshal the Objective-C object 0x7fff72f6b0d0 (type: __NSStackBlock__). Could not find an existing managed instance for this object, nor was it possible to create a new managed instance (because the type 'Foundation.NSError&' does not have a constructor that takes one IntPtr argument).

Repro:

1. Create a Document based app
2. Set the target framework to Xam Mac 4.5
3. Run the app and create a new doc
4. Duplicate the document (Shift+Cmd+S)

Result: Crash
Expected: Duplicated document

My Specific Repro:

1. Checkout the "uni" branch of Mecha https://github.com/praeclarum/Mecha/tree/uni
2. Open Mecha.Mac.sln
3. Run & Create a new Document
4. Duplicate the doc


Unhandled Exception:
System.Exception: Failed to marshal the Objective-C object 0x7fff72f6b0d0 (type: __NSStackBlock__). Could not find an existing managed instance for this object, nor was it possible to create a new managed instance (because the type 'Foundation.NSError&' does not have a constructor that takes one IntPtr argument).
   at ObjCRuntime.Runtime.MissingCtor (IntPtr ptr, IntPtr klass, System.Type type, MissingCtorResolution resolution) in /Users/builder/data/lanes/2097/f85a8459/source/maccore/src/ObjCRuntime/Runtime.cs:line 763
   at ObjCRuntime.Runtime.ConstructNSObject[T] (IntPtr ptr, System.Type type, MissingCtorResolution missingCtorResolution) in /Users/builder/data/lanes/2097/f85a8459/source/maccore/src/ObjCRuntime/Runtime.cs:line 786
   at ObjCRuntime.Runtime.GetNSObject (IntPtr ptr, System.Type target_type, MissingCtorResolution missingCtorResolution, Boolean evenInFinalizerQueue, System.Boolean& created) in /Users/builder/data/lanes/2097/f85a8459/source/maccore/src/ObjCRuntime/Runtime.cs:line 966
   at ObjCRuntime.Runtime.GetNSObjectWithType (IntPtr ptr, IntPtr type_ptr, System.Boolean& created) in /Users/builder/data/lanes/2097/f85a8459/source/maccore/src/ObjCRuntime/Runtime.cs:line 478
   at ObjCRuntime.Runtime.get_nsobject_with_type (IntPtr obj, IntPtr type, System.Boolean& created) in /Users/builder/data/lanes/2097/f85a8459/source/maccore/runtime/Delegates.generated.cs:line 208
   at (wrapper native-to-managed) ObjCRuntime.Runtime:get_nsobject_with_type (intptr,intptr,int&)
   at (wrapper managed-to-native) AppKit.NSApplication:NSApplicationMain (int,string[])
   at AppKit.NSApplication.Main (System.String[] args) in /Users/builder/data/lanes/2097/f85a8459/source/maccore/src/AppKit/NSApplication.cs:line 94
   at Mecha.Mac.main.main (System.String[] args) in /Users/fak/Dropbox/Projects/Mecha.Try9/Mecha.Mac/AppDelegate.fs:line 21
[ERROR] FATAL UNHANDLED EXCEPTION: System.Exception: Failed to marshal the Objective-C object 0x7fff72f6b0d0 (type: __NSStackBlock__). Could not find an existing managed instance for this object, nor was it possible to create a new managed instance (because the type 'Foundation.NSError&' does not have a constructor that takes one IntPtr argument).
   at ObjCRuntime.Runtime.MissingCtor (IntPtr ptr, IntPtr klass, System.Type type, MissingCtorResolution resolution) in /Users/builder/data/lanes/2097/f85a8459/source/maccore/src/ObjCRuntime/Runtime.cs:line 763
   at ObjCRuntime.Runtime.ConstructNSObject[T] (IntPtr ptr, System.Type type, MissingCtorResolution missingCtorResolution) in /Users/builder/data/lanes/2097/f85a8459/source/maccore/src/ObjCRuntime/Runtime.cs:line 786
   at ObjCRuntime.Runtime.GetNSObject (IntPtr ptr, System.Type target_type, MissingCtorResolution missingCtorResolution, Boolean evenInFinalizerQueue, System.Boolean& created) in /Users/builder/data/lanes/2097/f85a8459/source/maccore/src/ObjCRuntime/Runtime.cs:line 966
   at ObjCRuntime.Runtime.GetNSObjectWithType (IntPtr ptr, IntPtr type_ptr, System.Boolean& created) in /Users/builder/data/lanes/2097/f85a8459/source/maccore/src/ObjCRuntime/Runtime.cs:line 478
   at ObjCRuntime.Runtime.get_nsobject_with_type (IntPtr obj, IntPtr type, System.Boolean& created) in /Users/builder/data/lanes/2097/f85a8459/source/maccore/runtime/Delegates.generated.cs:line 208
   at (wrapper native-to-managed) ObjCRuntime.Runtime:get_nsobject_with_type (intptr,intptr,int&)
   at (wrapper managed-to-native) AppKit.NSApplication:NSApplicationMain (int,string[])
   at AppKit.NSApplication.Main (System.String[] args) in /Users/builder/data/lanes/2097/f85a8459/source/maccore/src/AppKit/NSApplication.cs:line 94
   at Mecha.Mac.main.main (System.String[] args) in /Users/fak/Dropbox/Projects/Mecha.Try9/Mecha.Mac/AppDelegate.fs:line 21

=== Xamarin Studio ===

Version 5.10 (build 800)
Installation UUID: e51e2705-c52a-4ae8-aa07-c481d6ab8a7a
Runtime:
	Mono 4.2.1 (explicit/4a18c57)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 402010056

=== Xamarin.Profiler ===

Version: 0.22.0.0
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Xamarin.Android ===

Version: 6.0.0.2 (Enterprise Edition)
Android SDK: /Users/fak/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.3   (API level 10)
		4.0.3 (API level 15)
		4.4   (API level 19)
		5.0   (API level 21)

SDK Tools Version: 23.0.5
SDK Platform Tools Version: 21
SDK Build Tools Version: 20

Java SDK: /usr
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)

=== Xamarin Android Player ===

Version: 0.3.7
Location: /Applications/Xamarin Android Player.app

=== Apple Developer Tools ===

Xcode 7.0 (8223)
Build 7A218

=== Xamarin.iOS ===

Version: 9.2.0.67 (Enterprise Edition)
Hash: f85a845
Branch: master
Build date: 2015-09-23 10:34:01-0400

=== Xamarin.Mac ===

Version: 2.4.0.62 (Enterprise Edition)

=== Build Information ===

Release ID: 510000800
Git revision: 92083188182e8c4588153e0d63e99c1a7037e4b6
Build date: 2015-09-18 15:03:06-04
Xamarin addins: 97a862a9dcaa31e9044c8613ef42e0a588c8d012
Build lane: monodevelop-lion-cycle6

=== Operating System ===

Mac OS X 10.10.5
Darwin lepton.local 14.5.0 Darwin Kernel Version 14.5.0
    Wed Jul 29 02:26:53 PDT 2015
    root:xnu-2782.40.9~1/RELEASE_X86_64 x86_64
Comment 1 Chris Hamons 2015-10-02 10:37:37 UTC
@Frank - Is there a permissions issue with that depo? I can't access it with my github account (or signed out).
Comment 2 Frank A. Krueger 2015-10-02 11:44:34 UTC
It's a private repo. I will add you if you send your github user id. 

But you don't need my code. Just do a new document based app as I outlined in the repro. No guess work here, happens 100% of the time.
Comment 3 Chris Hamons 2015-10-02 14:14:58 UTC
Ah alright. I was just following the last repro w/o noticing the first one. 


I can confirm w/ the first set of steps.
Comment 4 Chris Hamons 2015-10-05 16:54:41 UTC
Hmm. I can't reproduce it now with the latest alpha 

Could you try this and see if it works for you (with latest alpha). 

https://www.dropbox.com/s/8ny69xe8pepz3lc/TestDocument2.zip?dl=0

If it does, and your use case fails, I'm 

https://github.com/chamons

on github.
Comment 5 Frank A. Krueger 2015-10-06 11:00:28 UTC
Ah, in order to repro the bug, you have to switch to

F#
Xamarin.Mac .NET 4.5 Framework

I rewrote your example, and this one crashes every time (attached).

Sorry I didn't know it depended on these.
Comment 6 Frank A. Krueger 2015-10-06 11:00:53 UTC
Created attachment 13203 [details]
Minimum Repro
Comment 7 Chris Hamons 2015-10-06 11:53:28 UTC
Hmm. I downloaded your repro, and built it with:

Xamarin.Mac
Version: 2.4.0.79 (Business Edition)

Launch. Duplicate. Name. No crash on my end.

I also tried 2.4.0.62 with no crash.

Downloaded Mecha. 

Monolith:Mecha donblas$ git branch
  master
* uni

New document. Clone.

Error Opening Document: System.Exception: parse error
   at Microsoft.FSharp.Text.Parsing.Implementation.popStackUntilErrorShifted@273[tok,a] (Microsoft.FSharp.Text.Parsing.Tables`1 tables, Microsoft.FSharp.Text.Lexing.LexBuffer`1 lexbuf, Microsoft.FSharp.Text.Parsing.Stack`1 stateStack, Microsoft.FSharp.Text.Parsing.Stack`1 valueStack, Microsoft.FSharp.Text.Parsing.AssocTable actionTable, Microsoft.FSharp.Core.FSharpOption`1 tokenOpt) in <filename unknown>:line 0
   at Microsoft.FSharp.Text.Parsing.Implementation.interpret[tok,a] (Microsoft.FSharp.Text.Parsing.Tables`1 tables, Microsoft.FSharp.Core.FSharpFunc`2 lexer, Microsoft.FSharp.Text.Lexing.LexBuffer`1 lexbuf, Int32 initialState) in <filename unknown>:line 0
   at Microsoft.FSharp.Text.Parsing.Tables`1[tok].Interpret[char] (Microsoft.FSharp.Core.FSharpFunc`2 lexer, Microsoft.FSharp.Text.Lexing.LexBuffer`1 lexbuf, Int32 startState) in <filename unknown>:line 0
   at ScadParser.start[a] (Microsoft.FSharp.Core.FSharpFunc`2 lexer, Microsoft.FSharp.Text.Lexing.LexBuffer`1 lexbuf) in /Users/donblas/Programming/Local/Mecha/Mecha.Core/Mecha.Core/ScadParser.fs:line 1547
   at Mecha.Serialization.MechaDocument.ReadScad (Mecha.MechaDocument this, System.IO.TextReader foo, System.String name) in /Users/donblas/Programming/Local/Mecha/Mecha.Core/Serialization.fs:line 805

I'm on 10.10.5 (14F27).

I'm wondering what is different...Am I missing something?
Comment 8 Frank A. Krueger 2015-10-06 15:11:56 UTC
Looks like you have to duplicate the document twice - dup the dup.

And my F#/Framework info was a red herring - this is reproing on both frameworks.

Here's a video: http://screencast.com/t/WERuo3ger

(Sorry about the Mecha crashes - it's in development ;-))
Comment 9 Chris Hamons 2015-10-06 16:47:21 UTC
@Frank - You should look at that video. It's like performance art made by your mouse. I'm not sure what happened.

I did the following with the current alpha:

Xamarin Studio
Version 5.10 (build 811)
Installation UUID: 1f14d076-afda-48ab-bd30-d95b64dfb565
Runtime:
	Mono 4.2.1 (explicit/804ddbc)
	GTK+ 2.24.23 (Raleigh theme)

Apple Developer Tools
Xcode 7.0 (8223)
Build 7A218

Xamarin.Mac
Version: 2.4.0.79 (Business Edition)

1) New Solution
2) New Cocoa App (C#)
3) Name - TestDup. Create document-based.
4) Create
5) Open Document.cs
6) Change last two functions to be:

https://gist.github.com/chamons/b454a59c5cbda5615317

7) Project options, XM 4.5 framework
8) Build
9) Debug
10) File -> Duplicate or use shortcut. Create 10 documents. No Crash
11) Tried to repeat for F#, but apparently the F# wizard doesn't let you make document based by default. Don't know enough F# to make an example.

What am I missing?
Comment 10 Frank A. Krueger 2015-10-06 17:41:23 UTC
Chris: I linked to an F# example that crashes - it's attached to this bug. Please try that.

What OS are you on? This is a 10.10.5 machine.
Comment 11 Frank A. Krueger 2015-10-06 17:41:40 UTC
Attached, it's attached, not linked. :-)
Comment 12 Chris Hamons 2015-10-06 17:44:29 UTC
I'm on 10.10.5 (14F27)

Ok. Your F# sample does crash for me. Could you attach a C# one you say also crashes?
Comment 13 Frank A. Krueger 2015-10-06 18:01:54 UTC
Oh thank God.

I must be losing it cause I can't get the C# version to crash anymore. Part of my mind must have been entwined in the performance art video.
Comment 14 Chris Hamons 2015-10-06 18:08:38 UTC
:laugh:

No problem. I'm going to update the bug to note that it's an F# issue.
Comment 15 Rolf Bjarne Kvinge [MSFT] 2015-12-09 09:28:50 UTC
This is the same as bug #30927 (and the workaround is the same as well, just apply [<System.Runtime.InteropServices.OutAttribute>] to every 'err' parameter).

*** This bug has been marked as a duplicate of bug 30927 ***