-
Notifications
You must be signed in to change notification settings - Fork 516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
warning: Tried to create a managed reference from an object that already has a managed reference #15089
Comments
The fact it's easier to dupe on macOS is because the bindings include custom code [Export ("removeFromSuperview")]
[PreSnippet ("var mySuper = Superview;", Optimizable = true)]
[PostSnippet ("if (mySuper != null) {\n\t#pragma warning disable 168\n\tvar flush = mySuper.Subviews;\n#pragma warning restore 168\n\t}", Optimizable = true)]
void RemoveFromSuperview (); AFAIK this is not needed anymore (since newrefcount become the default with the unified API). However I can't see/blame the source code from the era to confirm. If that's the case then |
Quick followupOn iOS the warning appears because there are calls to the IOW the call to reference: unoplatform/uno#8688 (comment) |
Using the following code, just after the call to const byte InFinalizerQueue = 16; // see NSObject2.cs
var poker = Unsafe.As<NSObjectMemoryRepresentation>(this);
poker.flags = (byte)(poker.flags & ~InFinalizerQueue); |
It was added here: https://gist.github.com/rolfbjarne/b44c6e86bca3a9f46997a4d8f6a1aaf9 |
Remove code that we needed at some point due to memory related issues. These issues were fixed several years ago (newrefcount), so we no longer need this code (in fact it causes problems, see xamarin#15089).
I created a PR to remove those Pre/Post snippets: #15103. That doesn't fix the underlying problem though (resurrected managed peers only half resurrected), so I'll leave this bug open for now. |
note: this does not seems to be an issue for net6-macos, which uses coreclr and a different mechanism to get notified |
Remove code that we needed at some point due to memory related issues. These issues were fixed several years ago (newrefcount), so we no longer need this code (in fact it causes problems, see #15089).
Steps to Reproduce
NSObject
subclass is finalizedobject_queued_for_finalization
and setNSObjectFlagsInFinalizerQueue
in the instance's flagsDispose(false)
callsGC.ReRegisterForFinalize(this);
NSObjectFlagsInFinalizerQueue
setRemoveFromSuperview
)GetNSObject
is used to get the instance,TryGetNSObject
looks for one usingevenInFinalizerQueue: false
NSObjectFlagsInFinalizerQueue
is set the method returnsnull
xamarin_set_gchandle_with_flags_safe
will find aGCHandle
(because a managed peer exists) and returnfalse
Expected Behavior
The warning should not be printed and it the condition it checks should not occur.
Actual Behavior
The logs shows the warning
Tried to create a managed reference from an object that already has a managed reference
. SomeNSObject
instances are resurfaced needlessly since they already exists in managed land.Environment
The issue exists in
master
Notes
Build Logs
Not useful.
Example Project (If Possible)
It's easier to repro using Xamarin.Mac since
NSView.RemoveFromSuperview
calls theSuperview
property. However the warning is also seen on iOS but the conditions might be a little different.Building Uno SampleApp for macOS and selecting the Border sample will print the warning. The generated
Dispose
can be seen in the generator code.If needed I'll try to create a smaller sample. I had no luck earlier (on iOS) but I had not yet found the root issue.
The text was updated successfully, but these errors were encountered: