Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What could cause memory corruption in JavaScript?

I am developing an application which uses TinyMCE editors to allow the user to modify content. I have developed a Prototype.js class which, upon initialization, generates a unique ID and creates a TinyMCE editor on a <div> with that ID. Here's some of the relevant code:

Region = Class.create(
{
  initialize: function(options)
  {
    this._editorReady = false;
    this._index = Region.nextIndex++;
    this._uid = com.digitalfruition.Customizer.TypeRegion.uidPrefix+this._index;
    Region.instances.push(this);
    this.setupTinyMCE();
  },

  uid: function()
  {
    return this._uid;
  },

  index: function()
  {
    return this._index;
  },

In a lot of places, I will refer to a Region's uid(), for example, when events occur within the TinyMCE editor (for example, the selection changes) my handlers are given the ID of the editor. I will often do:

var index = Region.instances.invoke('uid').indexOf(uid);

To find the actual Region instance reflecting the interface the events occurred in.

I only ever set the _uid property of any given Region once. I'm sure of this.

But in Firefox 9.0.1, I am seeing really odd behavior. When events fire, the editor they're firing on can't be found because index in the code above is -1. After inspecting in Firebug, I saw this:

Screen Capture of odd behavior

Those are debug messages from my code, logging various things (the height of the regions in this case) along with the region's this.uid() value. The memory appears to have gotten corrupted! and you can even see where it happens, the first circled log entry is correct, and the subsequent ones are wrong.

The same code works fine in Safari and Chrome. So far only Firefox 9.0.1 seems to be affected...

To inspect further, I ran this in the Firebug console:

>>> com.digitalfruition.Customizer.TypeRegion.instances.invoke('uid');
["�ᔮ��蒦ᓤ�瀀魳ᓪ�倀⛺ᓪ�怀�eg����遀"]

That makes no sense to me. What could cause memory corruption like this in JavaScript? How do I even go about troubleshooting such a situation?

like image 478
Josh Avatar asked Jan 25 '12 01:01

Josh


People also ask

Can JavaScript run out of memory?

js project is the “JavaScript heap out of memory” error. This error usually occurs when the default memory allocated by your system to Node. js is not enough to run a large project. The error is common whether you run your project on Windows, macOS, or a Linux distribution like Ubuntu.

How do I free up memory in JavaScript?

To release memory, assign the global variable to null . window. users = null; I want to make this article as easy to understand as possible.

What causes memory leak in Web application?

In computer science, a memory leak is a leak of resources when computer software incorrectly manages memory allocation. A memory leak occurs when your web application assigns memory and keeps using it even though it is no longer needed.

What is memory corruption in Computer Science?

Memory corruption occurs in a computer program when the contents of a memory location are modified due to programmatic behavior that exceeds the intention of the original programmer or program/language constructs; this is termed violating memory safety. The most likely cause of memory corruption is programming error.

Is your JavaScript leaking memory?

The leaking JavaScript code is not in any way considered invalid, and the browser will not throw any error while running it. If we notice that our page's performance is getting progressively worse, the browser's built-in tools can help us determine if a memory leak exists and what objects cause it.

How to debug memory corruption errors?

Many memory debuggers such as Purify, Valgrind, Insure++, Parasoft C/C++test, AddressSanitizer are available to detect memory corruption errors.

What is memory corruption and buffer overflow?

Memory corruption. Buffer overflow is one of the most common programming flaws exploited by computer viruses, causing serious computer security issues (e.g. return-to-libc attack, stack-smashing protection) in widely used programs. In some cases programs can also incorrectly access the memory before the start of a buffer.


1 Answers

Is it possible that one of your files is saved with a different encoding? I'd look at that, and also maybe make sure you're serving the files with the correct encoding and mime types.

It might be a good idea to do some testing where you console.log() the uid values as they are generated to confirm that the corruption is in fact occurring after the objects are created. I suspect it's happening when they are created.

like image 61
Josh Earl Avatar answered Sep 29 '22 14:09

Josh Earl