向量成员已重置且无法访问 [英] A vector member is reset and unaccessible
问题描述
我有两个项目,一个基本客户端和一个动态库. 这是客户端中发生的事情:
I have two projects, one basic client and a dynamic library. Here's what happens in the client:
int main()
{
Scrutinizer scru;
scru.Scrutinize();
return 0;
}
在DLL中,Scrutinizer
类就是这样的(__ declspec(dllexport),为了清晰起见,省略了此类)
In the DLL, The Scrutinizer
class is as such (__declspec(dllexport) and such omitted for Clarity)
标题
class ProcessesGenerator;
class Scrutinizer
{
public:
Scrutinizer();
~Scrutinizer();
ProcessesGenerator *ProcGenerator
void Scrutinize();
};
ProcessesGenerator
的前向声明对我来说是强制性",以避免某种形式的循环引用.
The forward declaration of ProcessesGenerator
was 'mandatory' for me to avoid some kind of circular reference.
.cpp文件中的构造函数
这是我如何初始化它:
Scrutinizer::Scrutinizer()
{
ProcGenerator = &ProcessesGenerator();
}
有关此ProcessesGenerator
类的更多信息:
标题
class ProcessesGenerator
{
public:
ProcessesGenerator();
~ProcessesGenerator();
WinFinder winFinder;
std::vector<std::string> fooCollec;
void GenerateProcesses();
};
ProcessesGenerator.cpp
构造函数:
ProcessesGenerator::ProcessesGenerator()
{
//winFinder = WinFinder();//problem will be the same with or without this line
fooCollec = std::vector<std::string>{"one", "two", "three"};
}
构造函数中的断点表明,向量已使用选定的值进行了初始化.
A breakpoint in the constructor shows that the vector is initialized with the chosen values.
问题功能:
void ProcessesGenerator::GenerateProcesses() {
std::string foo = "bar";
fooCollec = std::vector<std::string>{};//read access violation
fooCollec.push_back(foo);//read access violation
winFinder.SomeVector= std::vector<std::string>{};//read access violation
}
在那里,我可以看到vector的大小被重置为0.任何尝试重新初始化它或推入元素的操作都会导致读取访问冲突.与它的WinFinder
成员的vecotr成员相同.我想这个缺陷很明显,但是我真的不明白,
Once there, I Can see that the size of vector is reset to 0. Any attempt to re-initialize it, or to push an element results in read access violation .Same with the vecotr member of its WinFinder
member. I guess the flaw is obvious, but I really don't get it,
谢谢!
推荐答案
您的问题出在
Scrutinizer::Scrutinizer()
{
ProcGenerator = &ProcessesGenerator();
}
您正在做的是获取一个临时对象的地址.该对象将被销毁,并在该行的末尾,您将获得一个指向有效对象的指针.
What you are doing is taking the address of a temporary object. That object will be destroyed and the end of that line and you will be left with a pointer that doesn't point to a valid object.
解决此问题的旧方法是使用
The old way to fix it would be to use
Scrutinizer::Scrutinizer()
{
ProcGenerator = new ProcessesGenerator();
}
但是现在您必须实现复制构造函数,复制赋值运算符和析构函数.由于您使用的是现代编译器,因此可以代替的是将ProcGenerator
设置为std:unique_ptr<ProcessesGenerator>
,然后将Scrutinizer()
变为
But now you have to implement the copy constructor, copy assignment operator, and the destructor. Since you have a modern compiler what you can do instead is make ProcGenerator
a std:unique_ptr<ProcessesGenerator>
and then Scrutinizer()
becomes
Scrutinizer::Scrutinizer() : ProcGenerator(make_unique<ProcessesGenerator>()) {}
我还想补充一点,&ProcessesGenerator();
甚至不应该编译.不幸的是,MSVS具有非标准扩展名,因此可以对其进行编译.您可以打开/Za
编译器选项(强制ANSI兼容性),然后会出现类似错误
I would also like to add that &ProcessesGenerator();
should not even compile. Unfortunately MSVS has a non-standard extension that allows this to compile. You can turn on the /Za
compiler option (enforce ANSI compatibility) and then you should get an error like
错误C2102:&"需要左值
error C2102: '&' requires l-value
这篇关于向量成员已重置且无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!