Skip to content

Conversation

dkimitsa
Copy link
Contributor

this fixes #608

root case is following code:

    public NSAttributedString(NSURL url, NSAttributedStringDocumentAttributes options) throws NSErrorException {
        super((SkipInit) null);
        long h = NSObject.alloc(ObjCClass.getByType(NSAttributedString.class));
        initObject(NSAttributedStringExtensions.init(ObjCObject.toObjCObject(NSAttributedString.class, h, NSObject.FLAG_NO_RETAIN), url, options, null));
    }

it creates two java object for same handle:

  • one with ObjCObject.toObjCObject.toObjCObject(ObjCObject() with option FLAG_NO_RETAIN (so it doesn't own native part)
  • and second NSAttributedString itself

but both of these do release a handle on dispose() that causes the crash

fix:

no need in creating intermediate Java object, -init like selectors should operate on pointer level

root case is following code:
```
    public NSAttributedString(NSURL url, NSAttributedStringDocumentAttributes options) throws NSErrorException {
        super((SkipInit) null);
        long h = NSObject.alloc(ObjCClass.getByType(NSAttributedString.class));
        initObject(NSAttributedStringExtensions.init(ObjCObject.toObjCObject(NSAttributedString.class, h, NSObject.FLAG_NO_RETAIN), url, options, null));
    }
```

it creates two java object for same handle:
- one with ObjCObject.toObjCObject.toObjCObject(ObjCObject() with option FLAG_NO_RETAIN (so it doesn't own native part)
- and second NSAttributedString itself

but both of these do release a handle on dispose() that causes the crash

fix: no need in creating intermediate Java object, `-init` like selectors should operate on pointer level
@Tom-Ski Tom-Ski merged commit 3daaa0d into MobiVM:master May 4, 2023
@dkimitsa dkimitsa deleted the fix/608-NSAttributedString-bindings-crash branch May 22, 2025 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Obtaining NSAttributedString causes crash on next GC
2 participants