为什么以下代码抛出
Exception thrown at 0x53A5C6DC (nvoglv32.dll) in RenderEngine.exe: 0xC0000005: Access violation reading location 0x0002B174.
Run Code Online (Sandbox Code Playgroud)
在运行时,什么是一个很好的解决方案?
std::vector<std::shared_ptr<Static>> statics;
void drawStatics() {
for (std::shared_ptr<Static> stat: statics) {
Static *statptr = stat.get();
statptr->Draw(); //This is what triggers the runtime exception.
}
}
void addStatic(Mesh &mesh, Texture &texture, Transform transform) {
statics.push_back(
std::make_shared<Static>(
mesh,
texture,
transform,
shader,
camera
));
}
int main() {
addStatic(playerMesh, playerTexture, platformTransform);
drawStatics();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Static头文件如下:
#pragma once
#include "mesh.h"
#include "texture.h"
#include "transform.h"
#include "camera.h"
#include "shader.h"
class …Run Code Online (Sandbox Code Playgroud)