0

My program which is an OpenGL program compiles and runs fine when i dont use memcopy, but when this function is used in the program it gives me a seg fault when i try to run the program after compilation, it still compiles in both cases but gives me the seg fault when i compile the program with this function, and the seg fault when checked in gdb does not show memcpy as the problem but an initialization function init() which creates the opengl context for me, but the weird thing is that it only happens when i include memcpy in the program and compile it, otherwise init() works fine , i also tested it in another file to confirm that it works on its own

I dont know why it is doing it, i did notice that this has happened since linux mint upgraded some packages, the program worked fine before the upgrade

here is the program source code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <GL/glew.h>
#include <GL/glx.h>

#include <time.h>
#include <math.h>

#include "glx2.c"
#include "renderBuffer.h"
#include "new.hpp"

#define WIDTH 1080 
#define HEIGHT 720

int main(){

   init(WIDTH,HEIGHT);
   createShaders();

   char name[1][25];

   float returned[720][1080][2] = {0.0f};


   strcpy(name[0],"ryu2.bmp");

   GLuint frameBuffer = createFramebuffer(frameBuffer);

   GLuint renderTexture = createRenderTexture(renderTexture, WIDTH,HEIGHT);
   //GLuint depth_texture;

   //glGenTextures(1, &depth_texture);
   //glBindTexture(GL_TEXTURE_2D, depth_texture);
   //glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT32F, 1080, 720);

   //glFramebufferTexture(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,depth_texture, 0);

   glFramebufferTexture(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,renderTexture,0);

   static const GLenum draw_buffers[] = { GL_COLOR_ATTACHMENT0 };
   glDrawBuffers(1, draw_buffers);


   //bind framebuffer 
   glBindFramebuffer(GL_FRAMEBUFFER,frameBuffer);
   checkFramebuffer();

   GLfloat vertices[] = { 

   //   
   //  X      Y                  U            V
   //triangle 1
     -1.0,       -1.0,          0.0,         0.0,
     -22.0/27.0, -1.0,          100.0/800.0, 0.0,
     -1.0,       -41.0/60.0,    0.0,         114.0/342.0,
     -22.0/27.0, -41.0/60.0,    100.0/800.0, 114.0/342.0};   


   GLuint vao1 = createVao();
   GLuint vbo1 = createVbo();

   glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);  

   GLuint tex = createTexture(name[0]);

   //set up data format in opengl and save in vao
   glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), 0);
   glEnableVertexAttribArray(0);

   glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (const GLvoid*)(2 * sizeof(GLfloat)));
   glEnableVertexAttribArray(1);


   bindObject(vao1, tex);

   glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
   glViewport(0,0, WIDTH,HEIGHT);

   glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

   //////completed drawing to framebuffer original values 

   glBindFramebuffer(GL_FRAMEBUFFER,0);

   glBindTexture(GL_TEXTURE_2D, renderTexture);

   glGetTexImage(GL_TEXTURE_2D, 0,GL_RG,GL_FLOAT,(void *)&returned);

   float another[720][1080][2];

   memcpy((void *)another, (const void *)returned, sizeof(returned));
   //----------------------completed copying original values to array for all comparison

   int i = 0,j=0;

   //for(j=0; j<114;j++)
   //   for(i=0; i<100;i++){   
         //printf("%f %f\n",another[j][i][0], another[j][i][1]);
   //}



   //from this point on there will be change and comparison

   //GLuint disp = glGetUniformLocation(shader_program, "disp");

   //glUniform2f(disp, 1.0/540,1.0/360);




   glXMakeCurrent( dpy, 0, 0 );
   glXDestroyContext( dpy, ctx );
   glXDestroyWindow(dpy, glxWin);
   XDestroyWindow( dpy, win );
   XFreeColormap( dpy, cmap );
   XCloseDisplay( dpy );

    return 0;

}

when i run gdb this is the problem it gives , despite that when i comment out memcpy it works fine and doesnt give me any seg faults

Program received signal SIGSEGV, Segmentation fault.
0x0000000000402876 in main () at untitled.cpp:22
22     init(WIDTH,HEIGHT);
(gdb) 
  • 9
    This is about 6 mega bytes: `float returned[720][1080][2]` then you declare another 6 mega bytes, it probably goes over stack limit. Try to allocate memory on heap. – Barmak Shemirani Jun 11 '16 at 04:49
  • @BarmakShemirani is right about the memory being allocated in the stack. Said that, gbd shows that your program crashed inside the init() function, is that correct? – ichramm Jun 11 '16 at 05:06
  • @ichramm thats correct sigsegv shows seg fault at init() but ONLY when memcpy in included, i ran another file with init() without any memcpy and it works fine, i also tested init() by itself and it works fine the sig fault is only when memcpy() is used so probably to do with overwriting the stack which must be overwriting the init() context creation – looking4answers Jun 11 '16 at 05:24
  • 1
    i just declared the array on heap as a pointer and it works fine now – looking4answers Jun 11 '16 at 05:26
  • @BarmakShemirani put your answer in the answer the question section and i will accept it as the correct answer – looking4answers Jun 11 '16 at 05:28
  • The reason the error disappears without memcpy is probably nothing to do with memcpy overwriting anything. It's just that that is the only line where you reference `another`, and when you comment that line out, the compiler probably sees that `another` is not used, and so optimizes it out, hence no stack overflow. And the only reason the debugger points to the `init` function, is because that's the first line of `main`, when the stack space is allocated. – Benjamin Lindley Jun 11 '16 at 05:29
  • @BenjaminLindley yeah same solution though, allocate to heap and problem is solved – looking4answers Jun 11 '16 at 05:31
  • i just seen that linux has a default stack limit of 8mb which i didnt know ,so that would definitely go over stack with the two arrays – looking4answers Jun 11 '16 at 05:35

2 Answers2

3

The answer as @BarmakShemirani pointed out is that linux has a stack limit of 8mb and since the two arrays are together over 12mb hence would overwrite the stack and hence why the problem occured for me, and the solution is to write to/allocate to the heap instead with malloc()

-1

@BarmakShemirani is correct about the stack limit. You can also increase the stack limit using setrlimit. in the code (but preferably not in the main function).

Community
  • 1
  • 1
merelyMark
  • 367
  • 3
  • 9