wrong link to destructor

  • Follow


there are two class with same name in an ugly program:

a.so
class REGION {

....
~REGION(){...}
}

b.so
class REGION {
....
~REGION(){}
}

where a.so's ~REGION is linked to b.so's ~REGION.

if i move the defination of ~REGION in a.so to a .cxx file, the ~REGION
in b.so will be linked to the ~REGION in a.so

what's wrong?
how to fixed?
(rename is NOT a practical idea!)


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply scl (2) 3/19/2005 1:02:02 AM

scl@ict.ac.cn wrote:

> what's wrong?

Violation of the One Definition Rule.

> how to fixed?
> (rename is NOT a practical idea!)

rename is the only portable answer.   Either change one or the other
or put it in a namespace to disambiguate it.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Ron 3/19/2005 10:46:05 AM


"scl@ict.ac.cn" <sangchunlei@gmail.com> wrote in message
news:1111152242.037007.29390@l41g2000cwc.googlegroups.com...
> there are two class with same name in an ugly program:

That breaks a fundamental rule. Say you declare:

Region region;

Which implementation should be called?

>
> a.so
> class REGION {
>
> ...
> ~REGION(){...}
> }
>
> b.so
> class REGION {
> ...
> ~REGION(){}
> }
>
> where a.so's ~REGION is linked to b.so's ~REGION.

That's impossible.

>
> if i move the defination of ~REGION in a.so to a .cxx file, the ~REGION
> in b.so will be linked to the ~REGION in a.so
>
> what's wrong?
> how to fixed?
> (rename is NOT a practical idea!)

Yes it is, use namespaces to seperate the decarations / implementations.

namespace A
{
    class Region
    {
        ....
    };
};

namespace B
{
    class Region
    {
        ....
    };
};


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply codigo 3/20/2005 11:56:52 PM

scl@ict.ac.cn wrote:
> there are two class with same name in an ugly program:

> a.so
> class REGION {

> ...
> ~REGION(){...}
> }

> b.so
> class REGION {
> ...
> ~REGION(){}
> }

> where a.so's ~REGION is linked to b.so's ~REGION.

> if i move the defination of ~REGION in a.so to a .cxx file,
> the ~REGION in b.so will be linked to the ~REGION in a.so

> what's wrong?
> how to fixed?
> (rename is NOT a practical idea!)

This isn't actually a C++ problem (since C++ doesn't have shared
objects), and is very dependant on the implementation.  Since
you speak of .so files, I will suppose Posix, where the answer
is to pass the RTLD_LOCAL flag to dlopen when you load the
objects.

--
James Kanze                                           GABI Software
Conseils en informatique orient�e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply kanze 3/21/2005 9:51:45 PM

scl@ict.ac.cn wrote:
> there are two class with same name in an ugly program:
>
> a.so
> class REGION {
>
> ...
> ~REGION(){...}
> }
>
> b.so
> class REGION {
> ...
> ~REGION(){}
> }
>
> where a.so's ~REGION is linked to b.so's ~REGION.
>
> if i move the defination of ~REGION in a.so to a .cxx file, the ~REGION
> in b.so will be linked to the ~REGION in a.so
>
> what's wrong?

In a standard C++ program, there must only be one definition of a
named entity with external linkage.  Unix dynamic linking normally
emulates static linking, so that this rule applies throughout all
shared objects.  If the rule is violated, anything can happen.

> how to fixed?
> (rename is NOT a practical idea!)

You need to limit which symbols are exported.  This is outside the
scope of standard C++.  You may find section 2.2 of
<http://people.redhat.com/drepper/dsohowto.pdf> helpful.

-- 
Ben Hutchings
Having problems with C++ templates?  Your questions may be answered by
<http://womble.decadentplace.org.uk/c++/template-faq.html>.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Ben 3/22/2005 9:07:43 AM

4 Replies
84 Views

(page loaded in 0.083 seconds)

Similiar Articles:












7/9/2012 3:54:43 PM


Reply: