Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > destructors mov...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 11 Topic 43690 of 47926
Post > Topic >>

destructors moved out of place ?

by vikimun@[EMAIL PROTECTED] Mar 5, 2008 at 03:53 AM

I have a question whether compiler can move destructors past
the place when it's normal out-of-scope place is. Is this true ?
Example:
I have a lock/unlock wrapped into the object SafeLock,

     where   SafeLock::SafeLock() { m_lock.Lock(); }
     and      SafeLock::~SafeLock() { m_lock.Unlock(); }
     void Class::Method(void) {
          SafeLock lock; // lock in ctor, unlocks in dtor
          ......
          // (1) lock is automatically unlocked here, ok.
     }

So far, so good. The lock is unlocked at point (1), when it goes out
of scope.

Now let's look at the more complex case, with innner block:

      void Class::Method(void) {
         ......
         { // inner block
                SafeLock lock; // (4)
         } // (5)
         ... // (6)
      }

Normally, lock(4) is destroyed at (5).

But I was told this is not necessarily so;
that C++ compiler is free to delay destruction of  lock
until later, until end of bigger bklock at (6).

Is this true ? Is it indeed allowd ? I have difficulty to believe
that
standard allows this. Indeed, destructors can have side effects like
closing files.

Is it possible to have standard reference that explicitly
prohibits this "optimization" ?

Viki
 




 11 Posts in Topic:
destructors moved out of place ?
vikimun@[EMAIL PROTECTED]  2008-03-05 03:53:32 
Re: destructors moved out of place ?
Michael DOUBEZ <michae  2008-03-05 13:59:17 
Re: destructors moved out of place ?
vikimun@[EMAIL PROTECTED]  2008-03-05 06:18:51 
Re: destructors moved out of place ?
peter koch <peter.koch  2008-03-05 06:48:00 
Re: destructors moved out of place ?
vikimun@[EMAIL PROTECTED]  2008-03-05 07:13:58 
Re: destructors moved out of place ?
Michael DOUBEZ <michae  2008-03-05 17:00:10 
Re: destructors moved out of place ?
vikimun@[EMAIL PROTECTED]  2008-03-05 08:05:54 
Re: destructors moved out of place ?
Micah Cowan <micah@[EM  2008-03-05 09:32:13 
Re: destructors moved out of place ?
peter koch <peter.koch  2008-03-05 10:13:25 
Re: destructors moved out of place ?
James Kanze <james.kan  2008-03-06 01:44:28 
Re: destructors moved out of place ?
vikimun@[EMAIL PROTECTED]  2008-03-12 23:20:44 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Oct 6 18:03:53 CDT 2008.