This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: std::function and shared object libraries
- From: Nick <nospam at codesniffer dot com>
- To: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- Cc: Gabriel Marcano <gabemarcano at yahoo dot com>, "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Fri, 19 Jun 2015 10:01:53 -0400
- Subject: Re: std::function and shared object libraries
- Authentication-results: sourceware.org; auth=none
- References: <CAH6eHdSn3gTWmAK-YDg90SqeoAUzuhDQFXv_bx5aOEQp0oiFiA at mail dot gmail dot com> <1638947916 dot 2267157 dot 1434207657547 dot JavaMail dot yahoo at mail dot yahoo dot com> <CAH6eHdSPa5=yEyGJi83TcK805y93cir5qF_6YRDZno6xCmx-xw at mail dot gmail dot com> <1434414075 dot 3436 dot 8 dot camel at nimble dot 325Bayport> <CAH6eHdQHCeuHPmspy9gnQPf7bdPiystE0maJFx2mFKc2Xh9qoA at mail dot gmail dot com> <1434458621 dot 3436 dot 24 dot camel at nimble dot 325Bayport> <CAH6eHdT=bm8Fxhmnn2DRVL_+6DB_uqpaag9UD--p7G37jDmvVQ at mail dot gmail dot com> <1434498156 dot 3436 dot 34 dot camel at nimble dot 325Bayport> <CAH6eHdR+X5FcDy8MNyhTL4GOQ-f4WhKVXpRd5-PyrzJc=Xhb_A at mail dot gmail dot com>
On Wed, 2015-06-17 at 09:35 +0100, Jonathan Wakely wrote:
> On 17 June 2015 at 00:42, Nick wrote:
> >
> > By "removed" I get the impression that it's somehow literally gone. For
> > example, a 4GB address space is now 3GB. The address space is still
> > there, it just contains undefined content which naturally will lead to
> > undefined behavior if accessed.
>
> Maybe it's just another problem with terminology but I would say it's
> literally gone. After the dlclose() there is nothing mapped at the
> address stored in the function pointer f._M_manager. It's not just
> garbage memory that produces undefined behaviour, it is not even a
> valid address. Trying to access that address causes a segmentation
> fault, raised by the operating system, because that address is not
> part of the process' virtual address space.
Now I'm glad I asked. I wasn't considering this aspect. Thanks.
> >> > So in the case
> >> > of the OP, that would amount to allocated objects (ex. function
> >> > template)
> >>
> >> Template instantiations are not allocated. They are code, not data.
> >
> > Right, in the case of a basic C++ function template. From the OP I was
> > thinking about something like std::function which I'd expect (though I
> > didn't look at the implementation) has some data along with it.
>
> But the problem has nothing to do with that data. The data is in the
> program's stack or heap, which doesn't disappear when you dlclose()
> the shared library. What disappears is the code for the function that
> the object will invoke from its destructor.
>
> This never had anything to do with memory allocation or where
> variables live in memory, only code.
Right, I get that the problem isn't related to the data. I was using
this opportunity to understand/confirm the effects of this scenario on
memory. I think it's clear to me now. Thanks for explaining and
teaching.