Hello,

I didn't express myself when changes were made to the interface of property
classes,
resulting in the change of string to const char * but now, I experiment many
segfault
that are a direct result of these changes.

While I totally agree with the fact that input parameters should be const
char * to
avoid unnecessary memory allocations, I thing that making the same changes
to return
values such as in getStringValue is a big mistake. Here is why :

First, I am trying to build latest CVS flightgear with MSVC6 on Win2k. I am
surprised
that these problems do not show up with gcc (linux or Win2k)

First example :

/sim/logging/classes is tied and its getter function is :

/**
 * Get the logging classes.
 */
static const char *
getLoggingClasses ()
{
  sgDebugClass classes = logbuf::get_log_classes();
  string result = "";
  for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
    if ((classes&log_class_mappings[i].c) > 0) {
      if (!result.empty())
        result += '|';
      result += log_class_mappings[i].name;
    }
  }
  return result.c_str();
}

the string result is a local variable and is not valid when getStringValue
returns.

Same problem with /sim/time/gmt-string and its getter function (from
fg_props.cxx) :

/**
 * Return the GMT as a string.
 */
static const char *
getGMTString ()
{
  string out;
  char buf[16];
  struct tm *t = globals->get_time_params()->getGmt();
  sprintf(buf, " %.2d:%.2d:%.2d",
   t->tm_hour, t->tm_min, t->tm_sec);
  // cout << t << " " << buf << endl;
  out = buf;
  return out.c_str();
}

Here again the string out is destroyed before the return value has a chance
to be copied !

There are tons of similar examples. A quick and dirty fix would be to
transform those
locals static but with the loss of reentrancy (bye-bye threaded flightgear
!)

The changes also introduced the need for the caller to allocate a string or
to call strcmp
that I consider ugly (it's a matter of taste) in C++ programs and that obfus
cate the code.

The only way to avoid these memory management problems is to return a class
that can hold
and free its memory. The standard string class can do that, or we can create
a new string
class that do copy on write with reference counters.

I don't want to advocate for a compiler or an other but I am still perplex
about the fact
that no one noticed these problems with gcc !

Cheers,

-Fred



_______________________________________________
Flightgear-devel mailing list
[EMAIL PROTECTED]
http://mail.flightgear.org/mailman/listinfo/flightgear-devel

Reply via email to