SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to prevent stack overflow?

    Is there any way to prevent this error, flush some memory maybe?
    Is it caused by logical coding error? (script runs error free except this)

  2. #2
    SitePoint Evangelist azizur_rahman's Avatar
    Join Date
    Nov 2001
    Location
    London, UK
    Posts
    502
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It seems to me you are going in a loop.

    What are you trying to do? Check all your code that, especilly the code that loops.

    Most likely you are tying to do something that it cant handle.

    Last time I had this error was looping with very long array of 3rd dimension.

    I dont see any reason why anyone want to do that in JavaScript/DHTML?
    Azizur Rahman
    Web Application Developer

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes indeed, it seems like it can be caused by 'bad' looping, or failing to stop a loop.
    I ran into this problem just before I had to leave so I posted this hoping I'd have somewhere to look by the time I got back to the computer.
    I was puzzled since I only made some minor changes and bam, this error.
    After some googling it turns out that the only possible cause given anywhere is recursion, or a function calling itself. Knowing that it was easy. Now I wasn't so stupid as calling the same function from within the function, but I had a bunch of functions calling eachother, and failing to reset some toggle... you get the picture.

    Here's some snips from the web:
    One possibility is that a Javascript function is calling itself, either directly or indirectly through another function. This is OK when you are writing a recursive function so long as there is a terminating condition. Otherwise the call stack just gets bigger and bigger until the browser runs out of memory on the stack.
    http://www.experts-exchange.com/Web/...0013705.html#1
    I figure this is solved by now:
    All versions of Microsoft Internet Explorer are vulnerable to a denial of service attack, caused by a stack overflow in the JavaScript settimeout() function. If a remote attacker creates a malicious Web site written in JavaScript that uses the settimeout() function, the Web browsers of visitors using Internet Explorer with JavaScript enabled would crash.
    Platforms Affected: Microsoft Internet Explorer All versions
    Remedy: No remedy available as of December 2001.
    http://www.iss.net/security_center/static/7661.php

    > It seems that MS Outlook 8.5.5104.6 screws up when displaying the
    > following string in a mail message:
    >
    > <javascript:location.reload()>
    >
    > Note that you do NOT need to click it, just displaying the mail
    > message will crash Outlook.
    >
    > The results are completely unpredictable, everything from hanging,
    > crashing or complaining about not being able to display a
    > particular font or complaining about being out of memory?!?!?!
    >
    > Anyone care to do anything fun with it other than spam mailing
    > people to create a big 'ole DoS? :-)
    http://archives.neohapsis.com/archiv...9-q4/0187.html
    From an interesting article about recursion and 'The Stack':
    Each time the function calls itself, it stores one or more variables on the Stack. Since the Stack holds a limited amount of memory, functions with a high recursive depth may crash because of non-availability of memory. Such a condition is known as Stack Overflow.
    Recursive functions usually have a terminating condition. In the above example the function stops calling itself when n==1. If this condition were not present, the function would keep calling itself with the values 3,2,1,0,-1,-2... and so on for infinity. This condition is known as Endless Recursion.
    http://www.siteexperts.com/tips/func...ts20/page1.asp

    Every time you call a function the current instruction pointer (a four-byte number) is pushed onto the stack (so that the program will know where to Return to at the end of the function). If you nest function calls (say 10000 deep), then this will result in 40,000 (40k) being pushed onto the stack which is typically bigger than the stack.
    The overhead of a function call in JS and VBS is terrific! Much more than you might think. Roughly as much as even calling a component method, for example. Thousands of machine instructions (vs. maybe a dozen or two if you are using C/C++).
    So if the recursive function simplifies your code *a lot* and this is not a performance-critical piece of your site...why not? Otherwise, avoid them.
    http://www.flws.com.au/showusyourcod...ve.asp?catID=1


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •