jinglei
jinglei

Reputation: 3353

c++ - cout of same variable but different output when accessed within or out of class

I'm building a simple Shader class. Shader class has a const char* _src member variable and is loaded in the constructor.

However, the outputs of

std::cout << _src << std::endl; // access within class

and

std::cout << fs._src << std::endl; // access out of class,fs is a Shader instance

are totally different. While the first gives correct result, the latter one output some meaningless character:

output 1

#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;

out vec3 color;

void main() {
        gl_Position = vec4(aPos, 1.0);
        color = aColor;
}

output 2

αG╬▀

Shader.h

class Shader {
public:
    const char *_src;
    GLuint _type;

    Shader(const char *fn, GLuint type);

private:
    bool loadSource(const char *fn);
};

Shader.cpp

#include "Shader.h"
#include <fstream>
#include <string>

#include <iostream>

Shader::Shader(const char *fn, GLuint type)
: _type(type) {
    loadSource(fn);
}

bool Shader::loadSource(const char *fn) {
    std::ifstream file(fn);
    if (!file.good())
        return false;

    std::string line, src;
    while (std::getline(file, line)) {
        src += line + '\n';
    }
    _src = src.c_str();

    std::cout << _src << std::endl;

    file.close();
    return true;
}

main.cpp

int main() {
    Shader fs("vertex_shader.glsl", GL_VERTEX_SHADER);
    std::cout << fs._src << std::endl;
}

The two cout appear in main() and Shader::loadSource().

vertex_shader.glsl

#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;

out vec3 color;

void main() {
    gl_Position = vec4(aPos, 1.0);
    color = aColor;
}

What is wrong??

Upvotes: 0

Views: 116

Answers (1)

Dan M.
Dan M.

Reputation: 4052

Classic example of "leaking" the address of the local: _src = src.c_str();. src is destroyed along with everything pointed at by _src once the loadSource call ends.

You need to either make a proper copy to _src, or use std::string directly instead to avoid such bugs.

Upvotes: 4

Related Questions