Reputation: 93
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.
Upvotes: 0
Views: 2326
Reputation: 15232
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.
Upvotes: 3
Reputation: 272
Can you please provide more information regarding the unhandled exception.
There is one obvious mistake in the above code. The insert_Fenster is inserting into a FensterManager instance and the reload_Fenster is trying to reload from a different instance of the FensterManager which is wrong.
void main()
{
FensterManager fm;
insert_Fenster(fm, 1,L"xyz");
reload_Fenster(fm, 1);
}
The insert and reload method should be updated as follows:
void insert_Fenster(FensterManager &fm, int pos,wstring name)
{
fm.insert(pos,name);
}
void reload_Fenster(FensterManager &fm, int pos)
{
if (fm.has(pos))
fm.get_Fenster(pos).reload();
}
Upvotes: 0
Reputation: 52659
looks like something inside void insert_Fenster(int pos,wstring name);
- look further up the call stack and you'll see which part of your code is calling into the runtime dll. That'll be the error.
Upvotes: 0