This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Patch: FYI: important memory-trashing fix


>>It turns out that in many cases we were passing the wrong length to
>>JvGetStringUTFRegion.  Perhaps we should add a new API that doesn't
>>take a length argument at all, since that seems to be the most common
>>use by far.
>
>Wow. At least it's not going to be me that's at the wrong end
>of Bryce's scrutiny this time. Lots of luck!
>
>http://gcc.gnu.org/ml/java-patches/2003-q3/msg00376.html
>
>:)

P.S. With my handy-dandy macro, here is an example of how
gnu::awt::gtk::GtkButtonPeer::setLabel can be rewritten.
(I'm typing this off the top of my head, so there may be
slight errors):

Current CVS Version:

void
gnu::awt::gtk::GtkButtonPeer::setLabel (java::lang::String *label)
{
  _Jv_GdkThreadLock sync;
  jsize len = 0;
  if (label)
    len = JvGetStringUTFLength (label);
  char buf[len + 1];
  // FIXME: this can allocate an unbounded amount.  Should use heap
  // even though it is slower.
  if (label)
    JvGetStringUTFRegion (label, 0, label->length(), buf);
  buf[len] = '\0';
  // The button child is a label.
  GtkBin *bin = GTK_BIN (ptr);
  gtk_label_set_text (GTK_LABEL (bin->child), buf);
}

Rewritten version:

static const char*
safe_cstr(const char* buf)
{
  return buf ? buf : "";
}

void
gnu::awt::gtk::GtkButtonPeer::setLabel (java::lang::String *label)
{
  _Jv_GdkThreadLock sync;
  JV_TEMP_UTF_STRING(utflabel, label);

  // The button child is a label.
  GtkBin *bin = GTK_BIN (ptr);
  gtk_label_set_text (GTK_LABEL (bin->child), safe_cstr (utflabel));
}




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]