1

I wrote a code in VC++ 2013, and when debugging it, the 'argc' argument of main function gets an abnormal big value. A value higher than 2,000,000,000 always!! But every time has value different from previous run. I checked the 'Command Arguments' field in project properties, it is empty.

How can i solve the problem?
If any more details is needed tell me.
Thanks.

the code:

#include <stdio.h>
#include <stdlib.h>

//for Windows, we use the static version of GLEW
#ifdef _WIN32
#define GLEW_STATIC
#endif

//GLFW and GLEW libraries
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "shader.h" // functions to load shader

//Global variables
GLFWwindow* window;

int main(int argc, char **argv)
{
    //Initialize GLFW
    if (!glfwInit()){
        fprintf(stderr, "Failed to initialize GLFW\n");
        exit(EXIT_FAILURE);
    }

    //enable anti-aliasing 4x with GLFW
    glfwWindowHint(GLFW_SAMPLES, 4);
    //specify the client API version that the created context must be compatible with.
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    //make the GLFW forward compatible
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    //use the OpenGL Core (http://www.opengl.org/wiki/Core_And_Compatibility_in_Contexts)
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    //create a GLFW windows object
    window = glfwCreateWindow(640, 480, "Chapter 4 - GLSL", NULL, NULL);
    if (!window){
        fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
        glfwTerminate();
        exit(EXIT_FAILURE);
    }
    //make the context of the specified window current for the calling thread
    glfwMakeContextCurrent(window);
    glfwSwapInterval(100);
    glewExperimental = true; // Needed for core profile
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Final to Initialize GLEW\n");
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    GLuint program = LoadShaders("simple.vert", "simple.frag");

    glBindFragDataLocation(program, 0, "color_out");
    glUseProgram(program);

    // Create Vertex Array Object
    GLuint vertex_array;
    glGenVertexArrays(1, &vertex_array);
    glBindVertexArray(vertex_array);

    // Create a Vertex Buffer Object and copy the vertex data to it
    GLuint vertex_buffer;
    GLuint color_buffer;

    glGenBuffers(1, &vertex_buffer);
    glGenBuffers(1, &color_buffer);

    const GLfloat vertices[] = {
        +1.0f, +0.0f, 0.0f,
        +1.0f, -1.0f, 0.0f,
        +0.0f, -1.0f, 0.0f,

        -1.0f, -0.0f, 0.0f,
        +0.0f, +1.0f, 0.0f,
        -1.0f, +1.0f, 0.0f,
    };
    const GLfloat colors[] = {
        0.0f, 0.0f, 1.0f,
        0.0f, 1.0f, 0.0f,
        1.0f, 0.0f, 0.0f,
        0.0f, 0.0f, 1.0f,
        1.0f, 0.0f, 0.0f,
        0.0f, 1.0f, 0.0f,
    };

    glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, color_buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);

    // Specify the layout of the vertex data
    GLint position_attrib = glGetAttribLocation(program, "position");
    glEnableVertexAttribArray(position_attrib);
    glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
    glVertexAttribPointer(position_attrib, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

    GLint color_attrib = glGetAttribLocation(program, "color_in");
    glEnableVertexAttribArray(color_attrib);
    glBindBuffer(GL_ARRAY_BUFFER, color_buffer);
    glVertexAttribPointer(color_attrib, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

    while (!glfwWindowShouldClose(window)){
        // Clear the screen to black
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        // Draw a rectangle from the 2 triangles using 6 vertices
        glDrawArrays(GL_TRIANGLES, 0, 24); //draw the square

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    //clear up the memories
    glDisableVertexAttribArray(position_attrib);
    glDisableVertexAttribArray(color_attrib);

    glDeleteBuffers(1, &vertex_buffer);
    glDeleteBuffers(1, &color_buffer);
    glDeleteVertexArrays(1, &vertex_array);

    glDeleteProgram(program);

    // Close OpenGL window and terminate GLFW
    glfwDestroyWindow(window);
    glfwTerminate();

    exit(EXIT_SUCCESS);
}

Edit:
I removed pieces of code till reached the following code. Interestingly, if I remove glewExperimental = true; from code, some link error (error LNK2001) rises!

#include <stdio.h>
#include <stdlib.h>

#ifdef _WIN32
#define GLEW_STATIC
#endif 

//GLFW library
#include <GL/glew.h>

int main(int argc, char **argv)
{
    glewExperimental = true; // Needed for core profile

    exit(EXIT_SUCCESS);
}
Erfan Razi
  • 15
  • 1
  • 7
  • 2
    what is the problem ? seems like you dont use the command line arguments – 463035818_is_not_an_ai Mar 04 '16 at 21:55
  • 1
    Does this also happen with an empty program? If not, remove pieces of code until you find the culprit. – Beta Carotin Mar 04 '16 at 21:55
  • 1
    This seems to point out you have been hitting undefined behavior in your code somewhere, trolling around on `main()`'s stack. – πάντα ῥεῖ Mar 04 '16 at 22:00
  • @tobi303 the problem is while i'm debugging, tracing line by line, the 'argc' has value over 2,000,000,000 (e.g. 2,165,324,527). The command line arguments are not used in this code, but I have another code also in OpenGL, that uses these arguments. Both of Programs has this problem. – Erfan Razi Mar 04 '16 at 22:09
  • What is the value of `argc` if you set a breakpoint on the opening `{` brace of `main`? – dxiv Mar 04 '16 at 22:20
  • @dvix each time has different value. one time: 2126176256 another run: 2138599424 ... – Erfan Razi Mar 04 '16 at 22:25
  • 3
    If you're debugging a release build, the values you see may not be accurate. – Mark Ransom Mar 04 '16 at 22:26
  • 3
    Unused. Might have been done away with by the compiler. What happens if you `std::cout << argc << std::endl;` to force it into play? – user4581301 Mar 04 '16 at 22:28
  • (1). Set a break point at the opening '{' of main. (2). Start debugging. (3). Click on `argc`. (4). Select `Debug` > `New Breakpoint` > `New Data Breakpoint` (5). Set "Byte Count" to 4 or 8, depending on whether you're building 32 or 64 bit. (6) Press f5. – kfsone Mar 04 '16 at 22:30
  • Configuration is on Debug, not Release. `cout << argc;` also writes that false number. – Erfan Razi Mar 04 '16 at 22:32
  • @kfsone I did this, `argc` doesn't change. So not have any break. – Erfan Razi Mar 04 '16 at 22:37
  • If you run the debug build, and a breakpoint on the main function shows garbage for `argc`, then either you have the wrong symbols (try a full rebuild), have some strange mixup of 32b and 64b compiles (that's unlikely to occur by simple mistake), or you link a library which has global objects whose constructors trash the stack. – dxiv Mar 04 '16 at 22:42
  • @dxiv Rebuild didn't solve. Configs are all on 32b, unless some configs are in code in gl libs. Your last guess may be true. I don't know exactly what you mean by trashing the stack, but in code I see an odd thing, if remove `glewExperimental = true;` from code, it rises a link error. ِDo you think it may be relevant? – Erfan Razi Mar 04 '16 at 23:02
  • The variable (and link error) are about GLEW which I am not familiar with. If you are worried about the _experimental_ sound of it, you can try to set `glewExperimental = false;` which should avoid the link error. – dxiv Mar 04 '16 at 23:09
  • 1
    Problem solved. In project properties, I had set the `entry point` to `main`. Removed it and made the `entry point` blank and `argc` now is `1`. Has anyone any idea what's the problem? – Erfan Razi Mar 04 '16 at 23:45
  • 4
    When you set the entrypoint to main() then you bypass the C runtime initialization code. The code that figures out what the proper values of argc and argv need to be and then calls main(). Please don't make random changes to the settings as provided by the project template. Dragons live there. – Hans Passant Mar 05 '16 at 00:19
  • Thanks @HansPassant I forgot to thank :) and I will avoid dragons ;) – Erfan Razi Apr 29 '16 at 11:42

0 Answers0