Jim Helman (jimh++at++surreal)
Sat, 16 Dec 95 12:46:03 -0800
Since Performer 2.0 does not overload any global new operators and
does not provide a global delete operator, there's no longer any
issue of providing your own global new and delete operators and using
them on your own objects, classes or native C++ types. But the array
delete problem described below still holds, since it is a feature of
the C++ language.
Performer 2.0 does restrict you from newing arrays of pfObjects, e.g.
pfGroups. This is a Performer feature.
You can new arrays of non-pfObjects, e.g. pfVec4's, but only from
the default shared memory arena. If you use an overloaded new
operator with an additional argument, e.g. the arena, for something
like pfVec4s, once again you cannot delete them.
This is a C++ feature.
> > Also, note that if you do overload a new operator whether
> > globally or locally in a class, DO NOT "new" arrays. The
> > following sequence with a user-defined new operator:
>
> > foos = new((arena *)NULL) foo[n];
> > delete[] foos;
>
> > is a really bad idea. The "new" works fine, but the "delete"
> > could end up invoking foo's destructor on the wrong pointers
> > some number of times (n: 0 <= n < 2<<31) and will then
> > attempt to free a bogus address.
So why is this a feature of the C++ language? There's a possibility
that an overloaded new operator with additional arguments is a
"placement" new operator, i.e. one which accepts a pointer to the
memory to be used as an argument. For standard new, C++ allocates
more than n*sizeof(foo) bytes of storage and squirreling the length
before the head of the returned array. But application code invoking
a "placement" new operator will probably only pass in n*sizeof(foo)
bytes of storage, so there's no place for C++ to store the length.
Requiring that extra storage be passed in would change C++ semantics
and break lots of code that uses "placement" news. But without the
extra storage C++ cannot know the length at deletion time. In fact,
during array deletion, some C++ implementations assume that the words
preceding the start of the array contain the length...... When the
overloaded new took an extra argument, these bytes are basically
random. This can lead to interesting results including invoking the
destructor many times, at incorrectly offset addresses and
potentially far outside the bounds of the array. Fun fun fun.
rgds,
-jim helman
jimh++at++surreal.asd.sgi.com
415/933-1151
This archive was generated by hypermail 2.0b2 on Mon Aug 10 1998 - 17:52:09 PDT