0xC0000005:访问冲突读取位置0x00000004,带有QStack <std :: map <std :: string,std :: string>&gt;追加</性病::地图<的std :: string,的std :: string>

时间:2012-11-18 14:55:14

标签: c++ visual-studio exception qt4

我有QStack&gt;这种情况一直在变得充满 从应用程序我用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看起来都不是空的或空的 还有什么呢?

2 个答案:

答案 0 :(得分: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 ,则您也需要在这些地方使用相同的互斥锁(例如,将其设为类成员)。

答案 1 :(得分:1)

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

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

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