Bug 37933 - bug in NSTextStorage binding?
Summary: bug in NSTextStorage binding?
Status: RESOLVED ANSWERED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 9.4 (iOS 9.2)
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: Untriaged
Assignee: Manuel de la Peña [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2016-01-22 20:07 UTC by bryan costanich
Modified: 2016-03-08 15:40 UTC (History)
3 users (show)

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


Attachments
repro (10.39 KB, application/zip)
2016-01-22 20:07 UTC, bryan costanich
Details
Fixed solution. (11.38 KB, application/zip)
2016-03-07 00:12 UTC, Manuel de la Peña [MSFT]
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 ANSWERED

Description bryan costanich 2016-01-22 20:07:34 UTC
Created attachment 14684 [details]
repro

Hey folks, 

I've got a head-scratcher. I'm writing a sample for folks to speed up their UITextView editing by optimizing the NSTextStorage class. however, i'm getting a really weird runtime error when overriding the LowLevelGetAttributes method telling me that `Objective-C exception thrown.  Name: NSRangeException Reason: NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds`

however, i'm just passing it to the underlying MSMutableAttributedString storage:

  override this.LowLevelGetAttributes(location, [<System.Runtime.InteropServices.OutAttribute>] outRange) = // : System.IntPtr
    let attrs = data.LowLevelGetAttributes (location, &outRange)
    attrs

and if put a breakpoint here, you'll see that the the range is actually fine. so i'm wondering if we have a binding bug? 

-

i've attached a simple repro project. to repro, run the project, and enter anything into the editor.


--

=== Xamarin Studio ===

Version 6.0 (build 3008)
Installation UUID: 6be4db6f-9150-4514-8d35-ce3356a9f542
Runtime:
	Mono 4.2.1 (explicit/6dd2d0d)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 402010102

=== Xamarin.Profiler ===

Not Installed

=== Xamarin.Android ===

Version: 6.0.0.34 (Business Edition)
Android SDK: /Users/bryancostanich/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.3   (API level 10)
		4.0.3 (API level 15)
		4.2   (API level 17)
		4.3   (API level 18)
		4.4   (API level 19)
		5.0   (API level 21)
		6.0   (API level 23)

SDK Tools Version: 24.4.1
SDK Platform Tools Version: 23.0.1
SDK Build Tools Version: 23.0.1

Java SDK: /usr
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)

=== Xamarin Android Player ===

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

=== Apple Developer Tools ===

Xcode 7.2 (9548)
Build 7C68

=== Xamarin.iOS ===

Version: 9.4.0.0 (Business Edition)
Hash: 7322991
Branch: master
Build date: 2015-12-08 16:20:29-0500

=== Xamarin.Mac ===

Version: 2.4.0.109 (Business Edition)

=== Build Information ===

Release ID: 600003008
Git revision: 864eb3914e62c3040c290995206b4e5f0b530f4f
Build date: 2016-01-19 15:22:57-05
Xamarin addins: 2efd4675ce97caf8d9b90a17a0018428c186c1c0
Build lane: monodevelop-lion-master

=== Operating System ===

Mac OS X 10.11.2
Darwin BryanLePuter.local 15.2.0 Darwin Kernel Version 15.2.0
    Fri Nov 13 19:56:56 PST 2015
    root:xnu-3248.20.55~2/RELEASE_X86_64 x86_64
Comment 1 Sebastien Pouliot 2016-01-22 21:56:32 UTC
not mono/AOT related
Comment 2 Sebastien Pouliot 2016-02-19 22:33:09 UTC
@Manuel can you have a look ? Minimally we want to make sure it's not a binding mistakes where we end up giving a count/length (that does not match reality) to the native API.
Comment 3 Manuel de la Peña [MSFT] 2016-03-06 21:10:59 UTC
Hello!

First of all, I must recognise that this got me confused too.. and I initially though it was a binding error, yet after reading the API carefully I noticed that the issue is due to a "race condition" that you are getting due to the fact of not using the event used to notify the layout manager when changes are made.

In order to get the correct index for a given range you need to use the BeginEditing and EndEditing methods to notify the API that changes are made. Please take a look at the following skeleton for the FastTextStorage you had that does the right notifications and will not have the exception raised. Also, if you do not use the notifications, you will get in an infinite loop of updates.

namespace Brightwood.FastTextStorage

open System
open Foundation
open UIKit
open ObjCRuntime

type FastTextStorage() as this=
  inherit NSTextStorage()

  let mutable data = new NSMutableAttributedString ()

  member this.SetText (text:NSMutableAttributedString) =
    System.Diagnostics.Debug.WriteLine("Set Text:" + text.ToString())

    // original edited range
    let editedRange = new NSRange(nint 0, data.Length)
    let delta = text.Length - data.Length

    // update
    data <- text

    // tell everyone stuff happened
    this.Edited(NSTextStorageEditActions.Attributes|||NSTextStorageEditActions.Characters, editedRange, delta)

  // we are overriding this because there was no method to overload that used NSString, and we wanted to be efficient here.
  // file a bug maybe?
  [<Export ("string")>]
  member this.NSStringValue : NSString =
    // optimization so we don't go NSSTtring :> System.String :> NSString
    let kung = Runtime.GetNSObject(data.LowLevelValue)
    match kung with
    | :? NSString as loretta -> loretta
    | _ -> new NSString("")

    //kung :?> NSString

  [<Export ("replaceCharactersInRange:withString:")>]
  member this.ReplaceCharactersInRange (range : NSRange, str : NSString) =
    base.BeginEditing ()
    // TODO: place to optimize, NSString to string
    data.Replace (range, new NSMutableAttributedString (str.ToString ()))
    let delta = str.Length - range.Length
    // TODO: place to optimize; if no reflow needed...
    this.Edited(NSTextStorageEditActions.Attributes|||NSTextStorageEditActions.Characters, range, delta)
    base.EndEditing () 


  override this.LowLevelGetAttributes(location, [<System.Runtime.InteropServices.OutAttribute>] outRange) = // : System.IntPtr
    let attrs = data.LowLevelGetAttributes (location, &outRange)
    attrs

  [<Export ("setAttributes:range:")>]
  member this.SetAttributes (attributes : NSDictionary, range : NSRange) =
    base.BeginEditing ()
    data.SetAttributes (attributes, range)
    this.Edited(NSTextStorageEditActions.Attributes, range, nint(0))
    base.EndEditing ()


Notice the usage of BeingEditing and EndEditing in the sections were the text will be edited. I do have a problem with the constructor for the NSMutableAttributedString taking a string and not an NSString forcing us to convert from NSString to string which is an operation that has some cost and calling the constructor with string might not be the most common pattern (we are probalby interacting with NSSTring always).

Sebastien, how bad would it be to add the NSString constructor so that it takes a NSString? Might be a nice improvement, otherwise, just changing the project code is all what is needed.

PS: Sorry for my F#, might not be the best one.
Comment 4 bryan costanich 2016-03-06 23:05:57 UTC
That doesn't solve it for me. I grabbed your code and dropped it into the repro project and I'm getting the exact same error as before.

Also, note that those methods are supposed to be optional, and Frank is using this class in a similar way, and he's not using the begin and end editing calls.

Did you change anything in the runtime to fix this?

This is my version info:

=== Xamarin Studio ===

Version 6.0 (build 4520)
Installation UUID: 6be4db6f-9150-4514-8d35-ce3356a9f542
Runtime:
	Mono 4.3.2 (mono-4.3.2-branch/ba2e5e4) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 403020467

=== Xamarin.Profiler ===

Not Installed

=== Apple Developer Tools ===

Xcode 7.2.1 (9548.1)
Build 7C1002

=== Xamarin.Mac ===

Version: 2.8.0.42 (Business Edition)

=== Xamarin.iOS ===

Version: 9.8.0.43 (Business Edition)
Hash: 4390115
Branch: master
Build date: 2016-03-03 18:17:12-0500

=== Xamarin.Android ===

Version: 6.0.99.544 (Business Edition)
Android SDK: /Users/bryancostanich/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.3   (API level 10)
		4.0.3 (API level 15)
		4.2   (API level 17)
		4.3   (API level 18)
		4.4   (API level 19)
		5.0   (API level 21)
		6.0   (API level 23)

SDK Tools Version: 24.4.1
SDK Platform Tools Version: 23.0.1
SDK Build Tools Version: 23.0.1

Java SDK: /usr
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)

=== Xamarin Android Player ===

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

=== Build Information ===

Release ID: 600004520
Git revision: a354b2cb9df4b96f50b0ccb960dbdd5a9155510c
Build date: 2016-02-18 14:52:07-05
Xamarin addins: 8e54979f23feee160d245a3c33e43e77779b1eb4
Build lane: monodevelop-lion-master

=== Operating System ===

Mac OS X 10.11.3
Darwin BryanLePuter.local 15.3.0 Darwin Kernel Version 15.3.0
    Thu Dec 10 18:40:58 PST 2015
    root:xnu-3248.30.4~1/RELEASE_X86_64 x86_64
Comment 5 Manuel de la Peña [MSFT] 2016-03-07 00:09:52 UTC
So, step by step:

> Also, note that those methods are supposed to be optional, and Frank is using this 
> class in a similar way, and he's not using the begin and end editing calls.

What methods are you talking about. As per Apple docs:

override init and define its own initialization methods, and implement the primitive methods of both NSAttributedString and NSMutableAttributedString. The primitive methods are:

string
attributesAtIndex:effectiveRange:
replaceCharactersInRange:withString:
setAttributes:range:

Which you can find here: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/TextStorageLayer/Tasks/Subclassing.html#//apple_ref/doc/uid/20000926-CJBBIAAF

Regarding BeginEditing and EndEditing, they must be added due to the way in which the UI processes the display, which is as follows:

1. The first stage is to send it a beginEditing message to announce a group of changes.
2. In the second stage, you send it some editing messages, such as deleteCharactersInRange: and addAttributes:range:, to effect the changes in characters or attributes. Each time you send such a message, the text storage object invokes edited:range:changeInLength: to track the range of its characters affected since it received the beginEditing message.
3. For the third stage, when you’re done changing the text storage object, you send it the endEditing message. This causes it to invoke its own processEditing method, fixing attributes within the recorded range of changed characters. (See Text Attribute Programming Topics for information about attribute fixing.)

Ref: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/TextStorageLayer/Tasks/ChangingTextStorage.html


Which means that the events MUST be triggered when the text is being changed.

The example change was tested in a clean VM with Xamarin Studio @ the following versions:

=== Xamarin Studio ===

Version 5.10.2 (build 56)
Installation UUID: f4527405-225e-4670-ad73-b81d32f4188b
Runtime:
	Mono 4.3.2 (mono-4.3.2-branch/905088b)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 403020475

=== Xamarin.Profiler ===

Not Installed

=== Xamarin.Android ===

Version: 6.0.1.10 (Starter Edition)
Android SDK: /Users/mandel/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		4.0.3 (API level 15)
		4.4   (API level 19)
		6.0   (API level 23)

SDK Tools Version: 24.4.1
SDK Platform Tools Version: 23.0.1
SDK Build Tools Version: 23.0.1

Java SDK: /usr
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)

=== Xamarin Android Player ===

Not Installed

=== Apple Developer Tools ===

Xcode 7.2 (9548)
Build 7C68

=== Xamarin.Mac ===

Version: 2.4.1.6 (Starter Edition)

=== Xamarin.iOS ===

Version: 9.4.1.25 (Starter Edition)
Hash: 962a050
Branch: master
Build date: 2016-01-29 16:59:11-0500

=== Build Information ===

Release ID: 510020056
Git revision: bb74ff467c62ded42b7b7ac7fdd2edc60f8647b0
Build date: 2016-01-26 16:24:41-05
Xamarin addins: 8b797d7ba24d5abab226c2cf9fda77f666263f1b
Build lane: monodevelop-lion-cycle6-c6sr1

=== Operating System ===

Mac OS X 10.11.3
Darwin mac-os-x-el-capitan.shared 15.3.0 Darwin Kernel Version 15.3.0
    Thu Dec 10 18:40:58 PST 2015
    root:xnu-3248.30.4~1/RELEASE_X86_64 x86_64

May I know how did you test it? I'll add the project I'm used as the fix.
Comment 6 Manuel de la Peña [MSFT] 2016-03-07 00:12:24 UTC
Created attachment 15280 [details]
Fixed solution.

Build and run in the simulator. You should be able to type with no exceptions raised.
Comment 7 Manuel de la Peña [MSFT] 2016-03-07 00:36:47 UTC
Same fix works on master:

=== Xamarin Studio ===

Version 5.10.2 (build 56)
Installation UUID: 01060673-5bee-4cf4-a4c2-5e36a18d39a2
Runtime:
	Mono 4.2.2 (explicit/996df3c)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 402020030

=== Xamarin.Profiler ===

Not Installed

=== Xamarin.Android ===

Version: 6.0.1.10 (Business Edition)
Android SDK: /Users/mandel/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		4.0.3 (API level 15)
		4.4   (API level 19)
		5.0   (API level 21)
		6.0   (API level 23)

SDK Tools Version: 24.4.1
SDK Platform Tools Version: 23.0.1
SDK Build Tools Version: 23.0.1

Java SDK: /usr
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)

=== Xamarin Android Player ===

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

=== Apple Developer Tools ===

Xcode 7.2.1 (9548.1)
Build 7C1002

=== Xamarin.Mac ===

Version: 2.9.0.98 (Business Edition)

=== Xamarin.iOS ===

Version: 9.9.0.98 (Business Edition)
Hash: 0d2c5c9
Branch: bug-38727
Build date: 2016-03-07 01:25:11+0100

=== Xamarin Inspector ===

Version: 0.4.0.0
Hash: 0bbef0d
Branch: master
Build date: Thu Dec 24 17:34:23 UTC 2015

=== Build Information ===

Release ID: 510020056
Git revision: bb74ff467c62ded42b7b7ac7fdd2edc60f8647b0
Build date: 2016-01-26 16:24:41-05
Xamarin addins: 8b797d7ba24d5abab226c2cf9fda77f666263f1b
Build lane: monodevelop-lion-cycle6-c6sr1

=== Operating System ===

Mac OS X 10.11.3
Darwin MandelsProWork 15.3.0 Darwin Kernel Version 15.3.0
    Thu Dec 10 18:40:58 PST 2015
    root:xnu-3248.30.4~1/RELEASE_X86_64 x86_64
Comment 8 bryan costanich 2016-03-07 20:13:46 UTC
Heya, thanks for this. Your sln works, but there are some other material code changes that I need to look through here to figure out why yours works, but just adding the BeginEditing() and EndEditing() calls doesn't.

I'll get back to you in a bit.
Comment 9 bryan costanich 2016-03-07 20:29:24 UTC
Still investigating, but it's interesting to note, that if you take the BeginEditing() and EndEditing() calls out of your fixed solution, it still works. So that's not the issue.

I think the issue comes from this:

 let editedRange = new NSRange(range.Location, str.Length - range.Location)

 this.Edited(NSTextStorageEditActions.Attributes|||NSTextStorageEditActions.Characters, editedRange, delta)
Comment 10 Manuel de la Peña [MSFT] 2016-03-08 09:12:31 UTC
I indeed changed that because the ranged passed to the method is already the one that is going to be edited and therefore is the one that should be raised in the event.. You  are correct (tested it) that Begin and End are not needed, I would add them nevertheless. 

Passing the ranges as range.Location, str.Length - range.Location can put create a range with the wrong size.. 

I think you are correct and the issue was firing the event with the wrong range, should we considered this bug closed??
Comment 11 bryan costanich 2016-03-08 15:39:23 UTC
Yes, thank you. Will be good to get this all doc'd.
Comment 12 bryan costanich 2016-03-08 15:40:06 UTC
closing it up