Can you guarantee destructor order when objects ar

2019-03-23 08:00发布

I have code that controls a mutex lock/unlock based on scope:

void PerformLogin()
{
    ScopeLock < Lock > LoginLock( &m_LoginLock );

    doLoginCommand();

    ScopeLock < SharedMemoryBase > MemoryLock( &m_SharedMemory );

    doStoreLogin();

    ...
}

Can I guarantee that MemoryLock will be destructed before LoginLock?

5条回答
太酷不给撩
2楼-- · 2019-03-23 08:05

Yes, destructors are called in the reverse order of construction.

查看更多
走好不送
3楼-- · 2019-03-23 08:06

Yes, destructors are the reverse of constructors. Because destructors are used to delete the objects which are no longer required and constructor are use to create the objects.

查看更多
我命由我不由天
4楼-- · 2019-03-23 08:12

Adding on to Neil's answer.

Consider if the opposite was true, that is that you couldn't predict the order of destructors for stack declared variables. That would make it nearly impossible to use dependent value types on the stack. Consider

void Foo() {
  Type1 t1;
  Type2 t2(&t1);
  ...
}

If C++ did not guarantee destructor ordering, straight forward code like this would be incredibly unsafe because it would be possible for t1 to be destroyed before t2's destructor ran. Hence you could not guarantee t2's destructor ran with a valid t1 value.

查看更多
Melony?
5楼-- · 2019-03-23 08:24

The question was answered already, but I'd like to add that I typically have a habit of writing something like this:

void PerformLogin()
{
    ScopeLock < Lock > LoginLock( &m_LoginLock );
    doLoginCommand();

    {
        ScopeLock < SharedMemoryBase > MemoryLock( &m_SharedMemory );
        doStoreLogin();
        ...
    }
}

In my opinion, this makes the intent clearer (*). That might be relevant if your code really is relying on the specific order. I find that this makes it less likely that someone accidentally changes the order, and causes a hard-to-find bug. (Well, that is of course a non-issue, since we all have tests in place, don't we?)

I always write the redundant parentheses in something like  (a && b) || c  too, and I find this matter quite similar.

(*): Of course, you could use a comment as well.

查看更多
6楼-- · 2019-03-23 08:26

Yes, it is. In any particular scope local objects are destroyed in the reverse order that they were constructed.

查看更多
登录 后发表回答