This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


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

Re: [I don't think it's off-topic at all] Linking speed for C++


On 31-May-2001, Michael Matz <matz@kde.org> wrote:
> one problem is our template KStaticDeleter, which serves as a type-safe
> delete mechanism for objects which are allocated in such a way:
> void f() {
>   static Object* o = 0;
>   if (!o) o = new Object;
>   ...
> }
> 
> Without further means o would be a program-exit memleak, so we have a
> template (for type-safety) which collects all such objects in a list,
> which then is cleaned at program exit.  It's used similar to this:
> static Deleter<Object> del;
> void f() {
>   static Object *o = 0;
>   if (!o) o = del.setObject(new Object);
>   ...
> }

I haven't seen the code for KStaticDeleter, but I'll bet that it
refers to a global or static member variable which contains the list of
objects to be deleted.

The point here is that not only must you not refer to global/static member
variables from inline functions, you must also not refer to them from
template functions, since template functions are instantiated in the
application's executable file.

If the global or static member variable in question is not a member of a class
template, then you can work around this by defining a non-inline access
function which returns a reference to the variable, and then using the
access function in place of the variable.

For static members of template classes, things may get more complicated,
especially if the template class is instantiated with the same type from
two different shared object files.

> Hmm.  I want a -Bsymbolic_for_func. ;)

That certainly sounds like it would be easier in the long run.

-- 
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.


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