0

我有 QStack > 一直被我用 QMutexLocker 保护它的应用程序填充,它从 QStack 是静态的不同线程填充。现在经过几次插入我得到:

0xC0000005: Access violation reading location 0x00000004  

这是出现异常的功能

bool MyClass::AppendToLinks(QVector<std::map<std::string,std::string > >& linksvec)
{
    try
    {

        static QMutex mutex;
        QMutexLocker locker(&mutex);

        //used to: be m_OuterLinksToProcessOutStack+= linksvec;

        QStack<std::map<std::string,std::string > > stackTemp;
        stackTemp = m_OuterLinksToProcessOutStack;
        stackTemp  << linksvec;
            // here is where the exception appear :
        m_OuterLinksToProcessOutStack = stackTemp;
        locker.unlock();
        if(m_OuterLinksToProcessOutStack.size()>0)
        {

            return true;
        }

        return false;
    }
    catch (std::exception const & e)
    {
        std::cout << "Standard exception: " << e.what() << std::endl;

    }
    catch (...)
    {
        std::cout << "Unknown exception." << std::endl;

    }
}

这是堆栈:

myapp.exe!std::_Tree<std::_Tmap_traits<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >,0> >::_Copy(const std::_Tree<std::_Tmap_traits<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >,0> > & _Right={...})  Line 1063 + 0x11 bytes    C++
    myapp.exe!std::_Tree<std::_Tmap_traits<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >,0> >::_Tree<std::_Tmap_traits<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >,0> >(const std::_Tree<std::_Tmap_traits<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >,0> > & _Right={...})  Line 536  C++
    myapp.exe!QVector<std::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,std::basic_string<char,std::char_traits<char>,std::allocator<char> > > > > >::realloc(int asize=12, int aalloc=12)  Line 532 + 0x1e bytes    C++
    myapp.exe!QVector<std::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,std::basic_string<char,std::char_traits<char>,std::allocator<char> > > > > >::operator+=(const QVector<std::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,std::basic_string<char,std::char_traits<char>,std::allocator<char> > > > > > & l={...})  Line 684    C++
    myapp.exe!MyClass::AppendToLinks(QVector<std::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,std::basic_string<char,std::char_traits<char>,std::allocator<char> > > > > > & linksvec={...})  Line 93   C++

QVector 和 Stacks 看起来都不是空的或 null 它还能是什么?

4

2 回答 2

2

如果mutex是保护访问m_OuterLinksToProcessOutStack,你应该保持对调用的锁if(m_OuterLinksToProcessOutStack.size()>0)。所以在try块内你想要这样的东西:

static QMutex mutex;
QMutexLocker locker(&mutex);
m_OuterLinksToProcessOutStack += linksvec;
return !m_OuterLinksToProcessOutStack.isEmpty();

不过,这看起来还是有点可疑。如果这是您的程序中唯一可以访问的地方m_OuterLinksToProcessOutStack,那么您可以将互斥锁作为本地静态对象。但如果它在其他任何地方访问,mutex将无法保护m_OuterLinksToProcessOutStack.

如果m_OuterLinksToProcessOutStack 其他地方访问,您也需要在这些地方拥有相同的互斥锁(例如,使其成为类成员)。

于 2012-11-18T15:22:57.213 回答
1

在我看来,m_OuterLinksToProcessOutStack正在另一个函数中访问(通过从 QStack 中提取链接的阅读器?)并且该位置不受互斥锁的保护。因此,当您尝试m_OuterLinksToProcessOutStack在另一个线程正在使用它时重新定义时,您会收到错误。

当你重新定义你的容器时,它必须首先销毁它以前的内容,然后再接受新的内容。如果当前正在使用容器,您可以看到为什么这可能是一个问题。为什么要创建一个临时堆栈而不是m_OuterLinksToProcessOutStack直接附加到?

此外,您在该函数中混合了 QVector 和 QStack。应该不是问题,但这可能取决于您以后如何访问它。

于 2012-11-19T21:13:59.333 回答