简体   繁体   English

构造函数在错误的时间调用

[英]Constructor called at wrong time

This is less of a plea for help over a problem, and more of a question about something peculiar discovered after solving a problem. 这不是对问题的求助请求,而是关于在解决问题后发现的特殊问题的更多问题。 I was working on my beginner OpenGL game and was attempting to write an OOP friendly rendering file. 我正在开始我的初学OpenGL游戏,并试图编写一个OOP友好的渲染文件。 All vbo data was being stored in a model class, where it was uploaded and buffered in the constructor. 所有vbo数据都存储在模型类中,并在构造函数中上载和缓冲。

Vastly simplified structure of this from main would look something like: 从main主要简化的结构看起来像:

int main(){
    vector <Model> Models;

    Graphics.GLInit();

    Models.push_back(Model(vertices,texcoords,36,0));

    Graphics.EnableAttributePointers();

    main loop
    {
        Graphics.draw(Models,Textures,Entities);
    }
    return 0;
}

Constructor looked like: 构造函数看起来像:

Model::Model(vector <vec3> &vertices,vector <vec2> &texcoords,int NumVertices,int StartVertex)
{
    iNumVertices=NumVertices;
    iStartVertex=StartVertex;

    Vertices=vertices;
    Texcoords=texcoords;

    glGenBuffers(1,&vboVertex);
    glGenBuffers(1,&vboTexcoord);
    glBindBuffer(GL_ARRAY_BUFFER,vboVertex);
    glBufferData(GL_ARRAY_BUFFER,Vertices.size()*12,Vertices.data(),GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER,vboTexcoord);
    glBufferData(GL_ARRAY_BUFFER,TexCoord.size()*8,TexCoord.data(),GL_STATIC_DRAW);
}

Upon calling glDrawArrays in the drawing function a segmentation fault would occur: 在绘图函数中调用glDrawArrays时,会发生分段错误:

0xC0000005 cannot access 0x00000000 yadda yadda.

So I checked over everything countless times, making sure it all made sense. 所以我无数次检查了一切,确保一切都有意义。 Eventually I tore apart the program, making a simpler version in which the same exact code is being used without a model class. 最终我把程序拆开了,制作了一个更简单的版本,其中没有模型类使用相同的精确代码。 That worked fine. 这工作得很好。 Figuring something was wrong with the Model class, I eventually placed the buffering code into a separate function to be called directly after the constructor. 在弄清楚Model类的错误时,我最终将缓冲代码放入一个单独的函数中,以便在构造函数之后直接调用。 This fixed it. 这解决了它。

Anyways, all this begs the question of why the constructor was seemingly being called upon out of sequence; 无论如何,所有这一切都引出了为什么构造函数似乎被按顺序调用的问题; before the GLInit code. 在GLInit代码之前。 That, or there is some behavioral quirk of constructors which I am unaware of, wherein the OpenGL state machine cannot be properly modified by them for some bizarre reason. 那个,或者有一些我不知道的构造函数的行为怪癖,其中OpenGL状态机由于某些奇怪的原因而无法被它们正确修改。 My knowledge of C++ is only spare time highschooler level, so am I missing something obvious here? 我对C ++的了解只是业余时间的高学历,所以我错过了一些明显的东西吗?

I am thinking since you are creating vector of Model, the following code 我在想,因为你正在创建Model的矢量,以下代码

Models.push_back(Model(vertices,texcoords,36,0));

actually makes a COPY (copy constructor will be called) pushes the copy into the vector. 实际上使COPY(将调用复制构造函数)将副本推送到向量中。 Then the original is deleted. 然后删除原件。

It would be interesting to see the actual data members of class Model and also what is the datatype of "vec3" and "vec2"? 看看类Model的实际数据成员以及“vec3”和“vec2”的数据类型是多么有趣?

Can we see the destructor function also? 我们还可以看到析构函数吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM