Bug 15405 - Only derived types are properly released
Summary: Only derived types are properly released
Status: RESOLVED FIXED
Alias: None
Product: MonoMac
Classification: Desktop
Component: Bindings ()
Version: unspecified
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-10-15 10:31 UTC by Richard Markiewicz
Modified: 2013-10-16 05:31 UTC (History)
4 users (show)

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


Attachments
Test Case (monomac solution + Cocoa framework) (57.66 KB, application/zip)
2013-10-15 10:31 UTC, Richard Markiewicz
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 FIXED

Description Richard Markiewicz 2013-10-15 10:31:24 UTC
Created attachment 5146 [details]
Test Case (monomac solution + Cocoa framework)

Based on the assumption that:
- When a managed wrapper is created for an Objective-C object, 'retain' is called on the ObcC object (and ref count is incremented by one)
- When the managed object is 'disposed', 'release' is called on the ObcC object (and ref count is decremented by one)
- When the ref count on the Objective-C object reaches 0, 'dealloc' is called

Given a simple Objective-C class like this:

- (id)init
{
    if(self = [super init])
    {
        NSLog(@"init");
    }
    
    return self;
}

- (void)dealloc
{
    NSLog(@"dealloc");
    
    [super dealloc];
}

- (NSString *)echo:(NSString *)message
// ...


Assume we generate a binding for this class called 'SimpleFramework' and use it in monomac:

// Derive a new class from SimpleFramework
public class DerivedSimpleFramework : SimpleFramework
{}

// Instantiate, call a method on, and dispose the vanilla SimpleFramework type
using(SimpleFramework sf = new SimpleFramework())
{
	Console.WriteLine(sf.Echo("Hello, world"));
}

GC.Collect();

// Instantiate, call a method on, and dispose the derived DerivedSimpleFramework type
using(DerivedSimpleFramework sf = new DerivedSimpleFramework())
{
	Console.WriteLine(sf.Echo("Hello, world"));
}

GC.Collect();

Expected behaviour:

'init' logged twice to console (once for each 'SimpleFramework' instantiation)
'dealloc' logged twice to console (once for each 'SimpleFramework' instantiation)

Actual output:

2013-10-15 10:19:02.696 SimpleBindingProject[1275:1007] init
2013-10-15 10:19:02.709 SimpleBindingProject[1275:1007] init
2013-10-15 10:19:02.709 SimpleBindingProject[1275:1007] dealloc

Note that 'dealloc' is only called once, via the derived 'DerivedSimpleFramework' managed class
Comment 1 Rolf Bjarne Kvinge [MSFT] 2013-10-16 05:31:37 UTC
This is a bug in Xamarin.Mac and MonoMac, which I've now fixed.

xamcore/master: 043724f832877e94a93465792542343a171eb4ca
monomac/master: ba5cfa2dc5a1623d379e8b4f7aa1a3efdf8a82a8