Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Question about 'root as you go' GC Tip
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Dave Wolfe (Skew Matrix)  
View profile  
 More options Jul 28 2008, 4:10 pm
Newsgroups: mozilla.dev.tech.js-engine
From: "Dave Wolfe (Skew Matrix)" <dwolfe5...@hotmail.com>
Date: Mon, 28 Jul 2008 13:10:27 -0700 (PDT)
Local: Mon, Jul 28 2008 4:10 pm
Subject: Question about 'root as you go' GC Tip
A project I'm working on recently ran into a garbage collection crash
that caused us to get super paranoid about how we're retrieving values
from JavaScript.  It's a multithreaded (JS_THREADSAFE) app, so we want
to be sure we're doing everything possible to avoid races/deadlocks/
etc.

We're pretty sure the crash turned out to be because of this bug:

  - https://bugzilla.mozilla.org/show_bug.cgi?id=438633

We've found a workaround for it, but we've since been having some
internal debates about whether code like the following is okay w.r.t.
garbage collection:

// Note: The calling code has already called JS_BeginRequest() on
'cx',
// so we don't bother doing it again here...
JSBool setFileName(JSContext* cx,
                   JSObject*, uintN argc,
                   jsval* argv,
                   jsval*)
{

  if (argc != 2 ||
      !JSVAL_IS_STRING(argv[0]) ||
      !JSVAL_IS_STRING(argv[1]) )
  {
    emitError(...);
    return JS_FALSE;
  }

  std::string streamName =
JS_GetStringBytes(JSVAL_TO_STRING(argv[0]) );
  std::string fileName = JS_GetStringBytes(JSVAL_TO_STRING(argv[1]) );

  ...

}

Specifically, the question is, can 'streamName' or 'fileName' ever get
garbage values?  Or can the app *crash* because we didn't root the
temporaries?

It has been suggested that it would be safer to rewrite the above two
assignments as:

  JSString* strStreamName = JS_ValueToString(cx, argv[0]);
  if (!strStreamName) { emitError(...); return JS_FALSE; }
  argv[0] = STRING_TO_JSVAL(strStreamName);

  JSString* strFileName = JS_ValueToString(cx, argv[1]);
  if (!strFileName) { emitError(...); return JS_FALSE; }
  argv[1] = STRING_TO_JSVAL(strFileName);

  std::string streamName = JS_GetStringBytes(strStreamName);
  std::string fileName = JS_GetStringBytes(strFileName);

I suspect that the extra six lines are simply a waste of space in this
case, but I've been wrong about JSAPI questions like this often enough
that I no longer trust my own judgement.  Is it okay to just say:

  std::string streamName =
JS_GetStringBytes(JSVAL_TO_STRING(argv[0]) );
  std::string fileName = JS_GetStringBytes(JSVAL_TO_STRING(argv[1]) );

and be done with it?

Any insight much appreciated...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mike Shaver  
View profile  
 More options Jul 31 2008, 9:40 am
Newsgroups: mozilla.dev.tech.js-engine
From: "Mike Shaver" <mike.sha...@gmail.com>
Date: Thu, 31 Jul 2008 06:40:18 -0700
Local: Thurs, Jul 31 2008 9:40 am
Subject: Re: Question about 'root as you go' GC Tip
On Mon, Jul 28, 2008 at 1:10 PM, Dave Wolfe (Skew Matrix)

<dwolfe5...@hotmail.com> wrote:
> Is it okay to just say:

>  std::string streamName =
> JS_GetStringBytes(JSVAL_TO_STRING(argv[0]) );
>  std::string fileName = JS_GetStringBytes(JSVAL_TO_STRING(argv[1]) );

> and be done with it?

Yes; anything on the JS stack is safe from collection.

Mike


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »