I talked to the developers about this one and I think we figured out what is happening. It has to do with the scope of the function. Here is what we believe is happening. You have defined your function on the Initialize event of an object. The function gets created and at the end of the Initialize event script it is no longer needed so it is marked for deletion. The function is still around until the next garbage collection cycle. Now the form finishes loading and you hit the button to call the function. Because th efunction still exists it is called. You continue to do this and all is OK. At some point garbage collection will run and the function will be deleted. It is at that point that you call it again and get the error. To resolve this problem you need to ensure that the funstion is always available. If you move your function to a script object then when the form loads the script object will run and your function will be created. Now it is callable at all times and shoudl alleviate th eissue you are having.
On another note I noticed the use of the "this" key word in your script. In our world the "this" object is the obejct that has focus (or who is making the call). The ability to extend the "this" object as you do with str, start and idx is going to disappear. The "this" object will become immutable in a future release. To ensure that you do not have issues in a future release I suggest you change the way you do that code.
Lastly by moving the code to a script object you will not be able to use "this". You can pass an object to the script and use that instead.
Hope that helps. Let me know if you need further explanation.