小编Lle*_*ewv的帖子

为什么在向量中的shared_ptr上调用方法会抛出运行时异常?

为什么以下代码抛出

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)

c++ shared-ptr runtimeexception

-3
推荐指数
1
解决办法
127
查看次数

标签 统计

c++ ×1

runtimeexception ×1

shared-ptr ×1