msvcr100d.dll中的未处理异常 [英] unhandled exception at msvcr100d.dll

查看:412
本文介绍了msvcr100d.dll中的未处理异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行时遇到了一个未处理的异常错误,当使用以下C ++代码时,可以使用Visual Studio使用debug跟踪错误。为什么会发生这个异常,你能解释一下吗?



首先我定义一个带有menname变量_name的类

  void insert_Fenster(int pos,wstring name); 
wstring get_Fenster_name(int pos);

class Fenster
{
public:
Fenster(wstring name)
:_name(name)
{}

void reload()
{
_name;
insert_Fenster(1,Ltemp);
wstring tmp = get_Fenster_name(1);
_name = tmp; //错误!!!
}

wstring get_name()
{
return _name;
}

private:
wstring _name;
};

其次我定义一个类来保存类的映射

  class FensterManager 
{
public:
bool has(int pos)
{
if(_mapFenster .find(pos)!= _ mapFenster.end())
return true;
else
return false;
}

void insert(int pos,wstring name)
{
if(has(pos))
{
_mapFenster.erase (pos);
}
_mapFenster.insert(make_pair(pos,Fenster(name)));
}

Fenster& get_Fenster(int pos)
{
return _mapFenster.at(pos);
}

private:
static map< int,Fenster> _mapFenster;
};

和一些util函数

  void insert_Fenster(int pos,wstring name)
{
FensterManager fm;
fm.insert(pos,name);
}

void reload_Fenster(int pos)
{
FensterManager fm;
if(fm.has(pos))
fm.get_Fenster(pos).reload();
}

wstring get_Fenster_name(int pos)
{
wstring name;

FensterManager fm;
if(fm.has(pos))
name = fm.get_Fenster(pos).get_name();

return name;
}

// main函数之前的静态成员的初始化
map< int,Fenster> FensterManager :: _ mapFenster;

这是主要功能

  void main()
{
insert_Fenster(1,Lxyz);
reload_Fenster(1);
}

Fenster类中的reload函数发生异常。 p>

有错误消息:
Regular_Expression.exe中的0x005cca34(msvcr100d.dll)未处理的异常:0xC0000005:访问冲突写入位置0xfeeefeee。

$ b $当调用 reload_Fenster(1); 时,它将调用 Fenster :: reload 。在该函数中,调用 insert_Fenster(1,Ltemp); FensterManager :: insert 将首先从地图中清除位置1。因此,当您返回到 Fenster :: reload 时,实例已被删除。一旦您尝试访问 _name ,您将尝试访问已删除的内存。



edit:
澄清;此函数调用:

  fm.get_Fenster(pos).reload 

将首先调用 fm.get_Fenster(pos)然后在结果上调用 reload()。如果 reload()函数中的 fm.get_Fenster(pos)变化,执行将不会移动新的Fenster,旧的将继续执行。即使你删除了旧的Fenster。



如果你还在运行函数,你应该确保不要删除一个实例。它会崩溃的应用程序,一旦你尝试访问成员,因为它们存储在已经删除的内存中。


I got a unhandled exception error at runtime when using the following C++ code, you can follow the error with debug using Visual Studio. Why will this exception happen, can you explain it?

First I define a class with a menber variable "_name"

void insert_Fenster(int pos,wstring name);
wstring get_Fenster_name(int pos);

class Fenster
{
public:
    Fenster(wstring name)
        :_name(name)
    {}

    void reload()
    {
        _name;
        insert_Fenster(1,L"temp");
        wstring tmp = get_Fenster_name(1);
        _name = tmp; //ERROR!!!
    }

    wstring get_name()
    {
        return _name;
    }

private:
    wstring _name;
};

Second I define a class to hold a map of the class

class FensterManager
{
public:
    bool has(int pos)
    {
        if (_mapFenster.find(pos)!=_mapFenster.end())
            return true;
        else
            return false;
    }

    void insert(int pos,wstring name)
    {
        if (has(pos))
        {
            _mapFenster.erase(pos);
        }
        _mapFenster.insert(make_pair(pos,Fenster(name)));
    }

    Fenster& get_Fenster(int pos)
    {
        return _mapFenster.at(pos);
    }

private:
    static map<int,Fenster> _mapFenster;
};

And some util functions

void insert_Fenster(int pos,wstring name)
{
    FensterManager fm;
    fm.insert(pos,name);
}

void reload_Fenster(int pos)
{
    FensterManager fm;
    if (fm.has(pos))
        fm.get_Fenster(pos).reload();
}

wstring get_Fenster_name(int pos)
{
    wstring name;

    FensterManager fm;
    if (fm.has(pos))
        name = fm.get_Fenster(pos).get_name();

    return name;
}

//Init of static member before main function
map<int,Fenster> FensterManager::_mapFenster;

that is the main function

void main()
{
    insert_Fenster(1,L"xyz");
    reload_Fenster(1);
}

The exception happen at "reload" function in class "Fenster".

With error message: Unhandled exception at 0x005cca34 (msvcr100d.dll) in Regular_Expression.exe: 0xC0000005: Access violation writing location 0xfeeefeee.

解决方案

When you call reload_Fenster(1);, it will call Fenster::reload. In that function you call insert_Fenster(1,L"temp");. FensterManager::insert will first erase position 1 from the map. So when you return to Fenster::reload, the instance has been deleted. As soon as you try to access _name, you try to access memory which has been deleted.

edit: To clarify; this function call:

fm.get_Fenster(pos).reload();

will first call fm.get_Fenster(pos) and then call reload() on the result. If fm.get_Fenster(pos) changes in the reload() function, execution will not move the new Fenster, but the old one will keep executing. Even if you delete the old Fenster.

You should make sure not to delete an instance if you are still running functions inside it. It will crash the application as soon as you try to access members, because they are stored in memory which has been deleted.

这篇关于msvcr100d.dll中的未处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆