I'm getting either 'unexpected error' or 'array index out of bounds' very much unexpectedly. What is odd is the changes that make the errors go away don't seem to make much sense. So the error happens in a "for string array in Skip" do loop where the key is an object. In each pass of this outer loop I get the associated key (the object), then I have an inner loop to iterate over the elements in the string array. At the moment, I am not including this code, but rather a function called from the inner loop when I process my string elements. The lower of the two functions below is called for each string I am processing. It uses the key object only, and returns text from other objects that link to this key object. QUESTION: Why does this fail when the second function below calls the first function? It doesn't fail if I replace the first function with the inline code, as shown in the comments in the second function.
Anyone have any ideas on this one?
//Remove newlines from string: string stripNewlines(string lines) { string oneLiner="" Regexp hasLine = regexp2 ".*" while (!null lines && hasLine lines) { oneLiner=oneLiner lines[match 0] lines=lines[end 0 + 2:] } return(oneLiner) } //For the given object o, if it has inlinks from another specific (open) module, return all the Object Text values of those other linked source objects: string getLinkedAlarmText(Object o) { Link inLink Object oSource int id string otxt="" string linkedAlarmText="" Regexp HasLine=regexp2 ".*" if (null o || null alarmModule) { return "" } for inLink in o <- "*" do { if ( ! null (oSource=source(inLink)) && module(oSource)==alarmModule) { id=oSource."Absolute Number" otxt=oSource."Object Text" //THIS DOES NOT WORK: otxt=stripNewlines(otxt) //REPLACE ABOVE FUNCTION CALL WITH THE FOLLOWING AND IT WORKS...WHY?: // //using an in-line version of stripNewlines() (since real function call caused issues for unknown reasons): // string lines=otxt // string oneLiner="" // while (!null lines && HasLine lines) { oneLiner=oneLiner lines[match 0]; lines=lines[end 0 + 2:] } // otxt=oneLiner otxt=trimOuterWhitespace(otxt) //now remove any leading and trailing whitespace linkedAlarmText=linkedAlarmText "\t\"" otxt "\"\n" } } return(linkedAlarmText) }
strathglass - Wed Apr 12 15:18:34 EDT 2017 |
Re: Yet another unexpected error or array index out of bounds If you are getting an array index out of bounds exception, chances are that they are really out of bounds. Especially looking at the code: lines=lines[end 0 + 2:] It is indeed possible, that (if your match occurs at the end), this produces an out of bounds error. You also need to be very careful with the expression - I had the problem that this expression is eventually parsed as end (0 + 2) = end 2 (which might give you unexpected results). So always do: lines = lines[2 + end 0]. And for the last line you should sheck, that (end 0 < length lines). As for the fact that inlining the function makes the error go away ... Problems like this can happen, when you declare a function twice. Search your complete codebase for stripNewlines (can be difficult especially inside encoded files), if you want to be sure rename the function to somethink less obvious. Another chance is, that your stack or something has been messed up by the rest of the code conaining a bug. This can produce random and weird errors, but most likely crashes not array index out of bounds errors. Regards, Mathias |
Re: Yet another unexpected error or array index out of bounds Thanks Mathias. I tried just changing "lines=lines[end 0 + 2:]" to "lines=lines[2 + end 0:]" (by the way, the former is the form used in the DXL example from the manual!): this alone didn't help. Also tried changing the function name to no avail. Can you clarify what you mean by "And for the last line you should check, that (end 0 < length lines)"?
The weird thing is I have two skips: I populate these with identically sized arrays, then in the "for string array in skip" loop where I get one array (the problem one), I use the object key to get the other corresponding array from its skip: I never have the out of bounds index problem with the other array.
And also printing out the array size just before the loop that causes the out of bounds index shows there should not be a problem, as per comments below in the code snippets shown: //Global skip list variables: Skip alarmReferences=create Skip alarmReferencesOrig=create //In one function to populate skips (above globals are arguments to this function): ... string alarmReferencesArray[alarmRefCount] string alarmReferencesArrayOrig[alarmRefCount] ... if (!put(alarmRefs,o,alarmReferencesArray)) { return(false) } if (!put(alarmRefsOrig,o,alarmReferencesArrayOrig)) { return(false) } ... //In the problem function: ... string alarmRefsArray[],alarmRefsArrayOrig[] //as shown above, these are always sized the same when dynamically sized in another routine that fills them and puts them in skip lists ... for alarmRefsArray in alarmReferences do { alarmRefCount=sizeof(alarmRefsArray) print "...current ARRAY SIZE=" alarmRefCount "\n" //********* INDICATES SIZE IS 2 BUT... o=(Object key alarmReferences) if (null o) { errorBox "ERROR - couldn't retrieve object key for current module \"" moduleFullName "\". Aborting."; abortScript() } if (!find(alarmReferencesOrig,o,alarmRefsArrayOrig)) { errorBox "ERROR - couldn't retrieve original alarm reference text! Aborting."; abortScript() } for (i=0; i<alarmRefCount; i++) { alarmRefTextOrig=alarmRefsArrayOrig[i] //******** ...NO ERROR HERE YET... alarmRefText=alarmRefsArray[i] //******** ...ARRAY BOUND EXCEEDED HERE WITH "array bounds exceeded (1)" EVEN THOUGH SIZE SHOULD BE TWO
|
Re: Yet another unexpected error or array index out of bounds strathglass - Thu Apr 13 07:16:22 EDT 2017 Thanks Mathias. I tried just changing "lines=lines[end 0 + 2:]" to "lines=lines[2 + end 0:]" (by the way, the former is the form used in the DXL example from the manual!): this alone didn't help. Also tried changing the function name to no avail. Can you clarify what you mean by "And for the last line you should check, that (end 0 < length lines)"?
The weird thing is I have two skips: I populate these with identically sized arrays, then in the "for string array in skip" loop where I get one array (the problem one), I use the object key to get the other corresponding array from its skip: I never have the out of bounds index problem with the other array.
And also printing out the array size just before the loop that causes the out of bounds index shows there should not be a problem, as per comments below in the code snippets shown: //Global skip list variables: Skip alarmReferences=create Skip alarmReferencesOrig=create //In one function to populate skips (above globals are arguments to this function): ... string alarmReferencesArray[alarmRefCount] string alarmReferencesArrayOrig[alarmRefCount] ... if (!put(alarmRefs,o,alarmReferencesArray)) { return(false) } if (!put(alarmRefsOrig,o,alarmReferencesArrayOrig)) { return(false) } ... //In the problem function: ... string alarmRefsArray[],alarmRefsArrayOrig[] //as shown above, these are always sized the same when dynamically sized in another routine that fills them and puts them in skip lists ... for alarmRefsArray in alarmReferences do { alarmRefCount=sizeof(alarmRefsArray) print "...current ARRAY SIZE=" alarmRefCount "\n" //********* INDICATES SIZE IS 2 BUT... o=(Object key alarmReferences) if (null o) { errorBox "ERROR - couldn't retrieve object key for current module \"" moduleFullName "\". Aborting."; abortScript() } if (!find(alarmReferencesOrig,o,alarmRefsArrayOrig)) { errorBox "ERROR - couldn't retrieve original alarm reference text! Aborting."; abortScript() } for (i=0; i<alarmRefCount; i++) { alarmRefTextOrig=alarmRefsArrayOrig[i] //******** ...NO ERROR HERE YET... alarmRefText=alarmRefsArray[i] //******** ...ARRAY BOUND EXCEEDED HERE WITH "array bounds exceeded (1)" EVEN THOUGH SIZE SHOULD BE TWO
OOh ... you are trying to put an array inside a skip ....That does not work unfortunately. You can only work with array pointers, but I would not recommend. You need to use a Skip List or something. Regarding the regexps: lines=lines[end 0 + 2:] At the last line, the match will match the whole line, that is (end 0) will point to the end of the string. Therefore lines[end 0 + 2:] will be beyond the end of the string, although I think this should result in an empty string rather than an out of bounds error. Regards, Mathias |
Re: Yet another unexpected error or array index out of bounds Mathias Mamsch - Thu Apr 13 10:13:32 EDT 2017 OOh ... you are trying to put an array inside a skip ....That does not work unfortunately. You can only work with array pointers, but I would not recommend. You need to use a Skip List or something. Regarding the regexps: lines=lines[end 0 + 2:] At the last line, the match will match the whole line, that is (end 0) will point to the end of the string. Therefore lines[end 0 + 2:] will be beyond the end of the string, although I think this should result in an empty string rather than an out of bounds error. Regards, Mathias OK, thanks for clarifying that important detail! :) Changed code to use a skip instead of an array and all is good now!
|