列表迭代器访问冲突
我有一个指向 Main
类中的 SomeClass
对象的列表
。在 Main
中,我可以使用 list::begin()
和 list::end()
迭代 list
>。
当我从 SomeClass
的实例执行相同操作时(请记住 list
是 Main
类的公共成员),我得到了以下异常:
0xC0000005:读取位置 0xbaadf0ad 时访问冲突。
我对 C++ 编码相当陌生,有 Python 背景,所以如果有些内容没有意义,我深表歉意。
以下是这种情况的示例:
编辑: Main 类代码实际上不在构造函数内,而是在名为 InitialiseObjects()
的方法中。
编辑:异常来自 list::being()
方法中的行 return (iterator(_Nextnode(_Myhead), this));
#include <list>
using namespace std;
class Main
{
public:
Main::InitialiseObjects()
{
// Execution starts here
SomeClass someClass = new SomClass(this);
someClasses.push_back(someClass);
// This works fine
for (list<SomeClass*>::iterator it = someClasses.begin(); it != someClasses.end(); it++)
...
someClass->AFunction();
}
list<SomeClass*> someClasses;
}
class SomeClass
{
public:
SomeClass::SomeClass(Main *main) : main(main) {}
void SomeClass::AFunction()
{
// This will not work throwing the aformentioned error
for (list<SomeClass*>::iterator it = main->someClasses.begin(); it != main->someClasses.end(); it++)
...
}
private:
Main *main;
}
I have a list
of pointers to SomeClass
objects in a Main
class. Whilst in Main
I can iterate over the list
using list::begin()
and list::end()
.
When I do the same from an instance of SomeClass
(bearing in mind that the list
is a public member of the Main
class) I get the following exception:
0xC0000005: Access violation reading location 0xbaadf0ad.
I am fairly new to coding in C++ coming from a Python background, so I apologise if something doesn't make sense.
Here is an example of the situation:
EDIT: The Main class codes is not actually inside the constructor, its in a method called InitialiseObjects()
.
EDIT: The exceptions comes from within the list::being()
method with the line return (iterator(_Nextnode(_Myhead), this));
#include <list>
using namespace std;
class Main
{
public:
Main::InitialiseObjects()
{
// Execution starts here
SomeClass someClass = new SomClass(this);
someClasses.push_back(someClass);
// This works fine
for (list<SomeClass*>::iterator it = someClasses.begin(); it != someClasses.end(); it++)
...
someClass->AFunction();
}
list<SomeClass*> someClasses;
}
class SomeClass
{
public:
SomeClass::SomeClass(Main *main) : main(main) {}
void SomeClass::AFunction()
{
// This will not work throwing the aformentioned error
for (list<SomeClass*>::iterator it = main->someClasses.begin(); it != main->someClasses.end(); it++)
...
}
private:
Main *main;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为那是
0xbaadf00d
,直到您覆盖其中的某些内容,这表明未初始化的堆内存。那么,也许您正在取消引用不存在的Main*
?例如,如果main
尚未初始化,main->someClasses.begin()
可能会抛出异常。事实上,这就是您的代码中的问题。
someClass->AFunction();
当您位于Main
构造函数内时调用该函数。因此,this
尚未完全有效,因此您无法在AFunction
内使用它。I think that was
0xbaadf00d
until you overwrote something in it, which indicates uninitialized heap memory. So, maybe you're dereferencing a non-existantMain*
? E.g.main->someClasses.begin()
may throw your exception, ifmain
wasn't initialized yet.And indeed, this is the problem in your code.
someClass->AFunction();
calls the function while you're inside theMain
-constructor. As such,this
will not be completely valid yet, so you can't use it inside theAFunction
.只要您留在
Main
构造函数中,this
实例就尚未完全初始化!尝试从其他地方访问实例会导致未定义的行为。由于您是从
SomeClass
中访问这个未完成的实例,因此会发生这种情况。As long as you remain inside the
Main
constructor, thethis
instance is not yet fully initialised!Trying to access the instance from elsewhere leads to undefined behaviour. This is happening here since you are accessing this unfinished instance from within
SomeClass
.