Skip navigation
Currently Being Moderated

Stack overrun

Aug 9, 2012 5:00 PM

Tags: #memory #xml #objects #stack_overrun

Hi

 

I’m working on a script that reads a text file, creates objects from tags it finds using a recursive function, pushes these objects into an array, and then adds these to the layout. It works for up to about 400 objects, but above that I get a “Stack overrun” error. Is this InDesign’s way of telling me it has run out of memory?

 

Here is the first part of my code:

 

var myFile = File.openDialog("Choose a data file");
var listingString = "listing";
var myArray = [];

if((myFile != "") && (myFile != null)){
    
    var sourceFile = new File(myFile);
    sourceFile.open();
    var myString = sourceFile.read();
    sourceFile.close();
    
    var endIndex;
    var listingContents;
    var foundTags;
    var listing;
    var tagStart;
    var tagEnd;
    var tagContent;
    
    function checkString(startIndex){
        endIndex = myString.indexOf("</" + listingString + ">", startIndex);
        if(endIndex != -1){
            listingContents = myString.slice(startIndex + 2 + listingString.length, endIndex);
            foundTags = listingContents.match(/<[a-z ]+>/gi);
            listing = {};
            for(i = 0; i < foundTags.length; i++){
                tagStart = listingContents.indexOf(foundTags[i]) + foundTags[i].length + 1;
                tagEnd = listingContents.indexOf("</" + foundTags[i].substring(1)) - 1;
                tagContent = listingContents.slice(tagStart, tagEnd);
                listing[foundTags[i].substr(1, foundTags[i].length - 2)] = tagContent;
            }
            myArray.push(listing);
            checkString(endIndex + 3 + listingString.length);
        }
    }
    
    checkString(myString.indexOf("<" + listingString + ">", 0));
    ...
}

 

Any suggestions on how I can fix it or optimise the code? I’m thinking of trying to split the string into multiple chunks and deal with one chunk at a time, but this seems like a clumsy workaround.

 

You may have guessed from the code that the source file I’m working with is XML, though because I don’t have much experience with XML and it seemed like much more work than what I’ve done here I went with this approach. I also thought this may be more flexible. Might working with it as true XML make a difference with the memory issue (if that’s what it is)?

 

I’m working in CS4 in Windows, but had the same problem in CS5 on a Mac that I tried.

 

I’d appreciate any help.

 
Replies
  • Currently Being Moderated
    Aug 10, 2012 2:19 AM   in reply to Graham van de Ruit

    This is the reason:

     

    function checkString {

    if (..)

    checkString

    checkString

    }

     

    -- in each call, it calls itself at least once -- and possible twice.

     

    Try using the native XML object wrapper instead! See http://jongware.mit.edu/idcs6js/pc_XML.html for the available functionality.

     

    var sourceFile = new File(...);
        sourceFile.open();
        var myString = sourceFile.read();
        sourceFile.close();
    myXml = new XML (myString);
    // length is a *function* in XML!
    alert (myXml.length());
    

     

    This is sort of a chimeara, half Javascript, half XML, but it sounds like you can do all operations you want through this interface.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points