DLL中的VC ++ vector :: push_back“访问冲突”

时间:2016-05-10 08:47:43

标签: c++ exception vector dll access-violation

我尝试将Win32应用程序转换为DLL。但是我对STL载体有一些困难。在原始应用程序中一切正常,但在DLL“访问冲突读取位置”错误发生。这是.h

的代码
    #ifdef NNETDLL_EXPORTS
    #define NNETDLL_API __declspec(dllexport)
    #else
    #define NNETDLL_API __declspec(dllimport)
    #endif

    #include <vector>

    using namespace std;
    #define VECARRAY vector<double>

    class NNETDLL_API CNNetDll 
    {
    public:
        CNNetDll(void);
        virtual ~CNNetDll();

        int m_InputNeurons, m_HiddenNeurons, m_OutputNeurons;

        /* Активаторы */
        vector<double> inputs;
        vector<double> hidden;
        vector<double> actual;

        /* Вход скрытых ячеек(со смещением) */
        vector<VECARRAY> who;

        /* Вход выходных ячеек(со смещением) */
        vector<VECARRAY> wih;

        int InitializeNetwork(CString FileName);
        void feedForward();
        void ActionNN(/*VECARRAY input_vec, VECARRAY& output_vec*/);
        void ReadNN(CString FileName);
    };

这是.cpp的代码,其中发生异常

int CNNetDll::InitializeNetwork(CString FileName)
{
    int i, hid, inp, out;
    CFile f;
    CString s;
    TCHAR szDrive[200];
    TCHAR szDir[200];
    TCHAR szFile[200];
    TCHAR szExt[200];

    _wsplitpath_s(FileName, szDrive, szDir, szFile, szExt);
    SetCurrentDirectory(szDir);
    s = szExt;  FileName = szFile + s;

    f.Open(FileName, CFile::modeRead);
    f.Read(&m_InputNeurons, sizeof(double));
    f.Read(&m_HiddenNeurons, sizeof(double));
    f.Read(&m_OutputNeurons, sizeof(double));

    for (i = 0; i < m_InputNeurons; i++)
    {
        inputs.push_back(0.0); // !!!!! ERROR IS HERE !!!!!!
    }
    for (i = 0; i < m_HiddenNeurons; i++)
    {
        hidden.push_back(0.0);
    }
    for (i = 0; i < m_OutputNeurons; i++)
    {
        actual.push_back(0.0);
    }
    for (i = 0; i < m_HiddenNeurons + 1; i++)
    {
        who.push_back(actual);
    }
    for (i = 0; i < m_InputNeurons + 1; i++)
    {
        wih.push_back(hidden);
    }
    for (hid = 0; hid < m_HiddenNeurons; hid++)
    {
        for (inp = 0; inp <= m_InputNeurons; inp++)
        {
            f.Read(&wih[inp][hid], sizeof(double));
        }
    }
    for (out = 0; out < m_OutputNeurons; out++)
    {
        for (hid = 0; hid <= m_HiddenNeurons; hid++)
        {
            f.Read(&who[hid][out], sizeof(double));
        }
    }

    f.Close();

    return 1;
}

该应用程序试图填充向量。 callstack的最后一步是在这个行的“xutility”文件中:

inline void _Container_base12::_Orphan_all()
    {   // orphan all iterators
 #if _ITERATOR_DEBUG_LEVEL == 2
    if (_Myproxy != 0)
        {   // proxy allocated, drain it
        _Lockit _Lock(_LOCK_DEBUG);

        for (_Iterator_base12 **_Pnext = &_Myproxy->_Myfirstiter;
            *_Pnext != 0; *_Pnext = (*_Pnext)->_Mynextiter) // !!!!LAST OPERATION BEFORE EXCEPTION!!!
            (*_Pnext)->_Myproxy = 0;
        _Myproxy->_Myfirstiter = 0;
        }
 #endif /* _ITERATOR_DEBUG_LEVEL == 2 */
    }

重复,在原始应用程序中,正常工作正常。另外我注意到,该向量产生了错误,该声明在CNNetDll类中首先出现。在上面的代码中,它是矢量“输入”。如果我改变这样的声明

vector<double> hidden;
vector<double> actual;
vector<double> inputs;

然后在此代码中使用“隐藏”向量出现异常

for (i = 0; i < m_InputNeurons; i++)
{
    inputs.push_back(0.0); //!!!! works properly !!!!!
}
for (i = 0; i < m_HiddenNeurons; i++)
{
    hidden.push_back(0.0); // !!! exception here !!!
}
for (i = 0; i < m_OutputNeurons; i++)
{
    actual.push_back(0.0);
}

对象创建和函数调用:

CNNetDll nn;
nn.InitializeNetwork(_T("M:\\Tasks\\2016\\Win8-64\\AI\\NNet\\Debug\\NN.dat"));

如何解决这个问题????

1 个答案:

答案 0 :(得分:1)

问题在于错误的变量定义(使用int数据类型)并将double数据类型的大小存储到这些变量中。匹配类型都可以很好地工作。

int m_InputNeurons, m_HiddenNeurons, m_OutputNeurons;