Exception OpenGL levée: violation d’access en lecture, la fenêtre était 0xCCCCCCCC

J’utilise le même cadre que celui décrit dans la question précédente.

Je l’ai résolu en créant une nouvelle DLL au lieu de simplement changer le type de construction du projet de ‘Application Windows (.exe)’ à ‘DLL (.dll)’.

Mais maintenant, lorsque j’utilise une variable de type GLFWwindow * dans ma structure et que j’essaie d’écrire ou de lire. Cela provoque toujours de faire apparaître l’access en écriture ou la violation d’access en lecture, respectivement. L’exception survient brutalement au moment où la fenêtre commence et se ferme, affichant l’exception.

L’exception dit ce qui suit: –

Exception levée: violation d’access en lecture.
fenêtre était 0xCCCCCCCC.

Cela se produit dans le fichier window.c de GLFW et pointe vers la fonction qui essaie de le lire. J’ai même essayé d’utiliser des pointeurs pour modifier la fenêtre, mais cela ne fonctionnait toujours pas.

Voici le code pour le cadre: – Modifié!

window.h

#ifndef LIB_GRAPHICS #define LIB_GRAPHICS #ifdef LIB_EXPORTS #define LIB_EXPORT _declspec(dllexport) #else #define LIB_EXPORT _declspec(dllimport) #endif #define LIB_FALSE 0 #define LIB_TRUE 1 #define LIB_BeginRender LIB_SetEvents(); LIB_ClearToColor #define LIB_EndRender LIB_SwapWindowBuffers #define LIB_CENTER_POSITION 0xCEAAFFEE /* Define other things... */ typedef const char* LIB_Ssortingng; typedef unsigned LIB_Integer; typedef char LIB_Char; typedef int LIB_Bool; /* Define the structures... */ typedef struct LIB_Window LIB_Window; typedef struct LIB_Color { int r; int g; int b; int a; } LIB_Color; /* Constructors, destructors and other functions... */ LIB_Bool LIB_EXPORT LIB_Initialize(); void LIB_EXPORT LIB_SetEvents(); void LIB_EXPORT LIB_ClearToColor(const LIB_Color color); void LIB_EXPORT LIB_SetFrameColor(const LIB_Color color); LIB_EXPORT LIB_Window* LIB_CreateWindow(const LIB_Ssortingng title, const int x, const int y, const int width, const int height, const LIB_Bool resizable, const LIB_Bool fullscreen); void LIB_EXPORT LIB_GetDisplaySize(int *width, int *height); void LIB_EXPORT LIB_GetWindowFrameSize(LIB_Window* window, int *width, int *height); void LIB_EXPORT LIB_GetWindowCursorPosition(LIB_Window* window, float *x, float *y); void LIB_EXPORT LIB_GetWindowPosition(LIB_Window* window, int *x, int *y); void LIB_EXPORT LIB_GetWindowSize(LIB_Window* window, int *width, int *height); LIB_Ssortingng LIB_EXPORT LIB_GetWindowTitle(LIB_Window* window); LIB_Bool LIB_EXPORT LIB_IsWindowFullScreen(LIB_Window* window); LIB_Bool LIB_EXPORT LIB_IsWindowOpened(LIB_Window* window); void LIB_EXPORT LIB_SwapWindowBuffers(LIB_Window* window); void LIB_EXPORT LIB_SetWindowPosition(LIB_Window* window, const int x, const int y); void LIB_EXPORT LIB_SetWindowSize(LIB_Window* window, const int width, const int height); void LIB_EXPORT LIB_SetWindowTitle(LIB_Window * window, const LIB_Ssortingng title); void LIB_EXPORT LIB_SetFullScreenState(LIB_Window * window, const LIB_Bool fullscreen); void LIB_EXPORT LIB_DestroyWindow(LIB_Window* window); void LIB_EXPORT LIB_Terminate(); #endif /* LIB_GRAPHICS */ 

window.c

 #include "window.h" #define GLEW_STATIC #include  #include  #include  #include  /* Create the structures... */ struct LIB_Window { LIB_Ssortingng title; int x; int y; int width; int height; LIB_Bool fullscreen; GLFWwindow** window; }; /* Start the functions here... */ LIB_Bool LIB_Initialize() { return (LIB_Bool)glfwInit(); } void LIB_SetEvents() { glfwPollEvents(); } void LIB_ClearToColor(const LIB_Color color) { glClearColor((float)color.r / 255, (float)color.g / 255, (float)color.b / 255, (float)color.a / 255); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); } void LIB_SetFrameColor(const LIB_Color color) { glClearColor((float)color.r / 255, (float)color.g / 255, (float)color.b / 255, (float)color.a / 255); } LIB_Window* LIB_CreateWindow(const LIB_Ssortingng title, int x, int y, const int width, const int height, const LIB_Bool resizable, const LIB_Bool fullscreen) { LIB_Window wind; wind.title = title; if (x == LIB_CENTER_POSITION) { const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor()); x = (mode->width - width) / 2; } wind.x = x; if (y == LIB_CENTER_POSITION) { const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor()); y = (mode->height - height) / 2; } wind.y = y; wind.width = width; wind.height = height; wind.fullscreen = fullscreen; glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); glfwWindowHint(GLFW_RESIZABLE, resizable); wind.window = NULL; if (fullscreen == 1) { wind.window = (GLFWwindow**)glfwCreateWindow(width, height, title, glfwGetPrimaryMonitor(), NULL); } else if (fullscreen == 0) { wind.window = (GLFWwindow**)glfwCreateWindow(width, height, title, NULL, NULL); } glfwSetWindowPos((GLFWwindow*)wind.window, x, y); int screen_width, screen_height; glfwGetFramebufferSize((GLFWwindow*)wind.window, &screen_width, &screen_height); if (wind.window == NULL) { glfwTerminate(); return NULL; } glfwMakeContextCurrent((GLFWwindow*)wind.window); glewExperimental = GL_TRUE; if (glewInit() != GLEW_OK) { return NULL; } glViewport(0, 0, screen_width, screen_height); glMasortingxMode(GL_PROJECTION); glPushMasortingx(); glLoadIdentity(); glMasortingxMode(GL_MODELVIEW); glEnable(GL_BLEND); glEnable(GL_ALPHA_TEST); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); return &wind; } void LIB_GetWindowFrameSize(LIB_Window * window, int *width, int *height) { int screenwidth, screenheight; glfwGetFramebufferSize(((GLFWwindow*)window->window), &screenwidth, &screenheight); *width = screenwidth; *height = screenheight; } void LIB_GetWindowCursorPosition(LIB_Window * window, float *x, float *y) { double cx, cy; glfwGetCursorPos(((GLFWwindow*)window->window), &cx, &cy); *x = (float)cx; *y = (float)cy; } void LIB_GetDisplaySize(int *width, int *height) { const struct GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor()); *width = mode->width; *height = mode->height; } void LIB_GetWindowPosition(LIB_Window * window, int *x, int *y) { *x = (window)->x; *y = (window)->y; } void LIB_GetWindowSize(LIB_Window * window, int *width, int *height) { *width = (window)->width; *height = (window)->height; } LIB_Ssortingng LIB_GetWindowTitle(LIB_Window * window) { return (window)->title; } LIB_Bool LIB_IsWindowFullScreen(LIB_Window * window) { return (window)->fullscreen; } LIB_Bool LIB_IsWindowOpened(LIB_Window * window) { return !glfwWindowShouldClose(((GLFWwindow*)window->window)); } void LIB_SwapWindowBuffers(LIB_Window * window) { glfwSwapBuffers(((GLFWwindow*)window->window)); } void LIB_SetWindowPosition(LIB_Window * window, const int x, const int y) { glfwSetWindowPos(((GLFWwindow*)window->window), x,y); (window)->x = x; (window)->y = y; } void LIB_SetWindowSize(LIB_Window * window, const int width, const int height) { glfwSetWindowSize(((GLFWwindow*)window->window), width, height); (window)->width = width; (window)->height = height; } void LIB_SetWindowTitle(LIB_Window * window, const LIB_Ssortingng title) { glfwSetWindowTitle(((GLFWwindow*)window->window), title); (window)->title = title; } void LIB_SetFullScreenState(LIB_Window * window, const LIB_Bool fullscreen) { const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor()); if (fullscreen == LIB_FALSE) { glfwSetWindowMonitor(((GLFWwindow*)window->window), NULL, (window)->x, (window)->y, (window)->width, (window)->height, 60); } else if (fullscreen == LIB_TRUE) { glfwSetWindowMonitor(((GLFWwindow*)window->window), glfwGetPrimaryMonitor(), 0, 0, mode->width, mode->height, 60); } (window)->fullscreen = fullscreen; } void LIB_DestroyWindow(LIB_Window * window) { (window)->window = NULL; (window)->title = NULL; (window)->x = 0; (window)->y = 0; (window)->width = 0; (window)->height = 0; free(window); } void LIB_Terminate() { printf("BLITZ terminated by the user!!\n"); glfwTerminate(); } 

La fonction LIB_CreateWindow renvoie un pointeur sur la variable LIB_Window wind; . Une fois la fonction terminée, la variable sort de la scope et le pointeur indique “nulle part”. C’est un comportement indéfini. Notez qu’une variable locale dans une fonction est allouée sur la stack, qui est immédiatement libérée lorsque la fonction est terminée.

Vous pouvez résoudre ce problème rapidement en déclarant la variable static :

 static LIB_Window wind; 

Mais si vous le faites, la bibliothèque ne peut bien sûr gérer qu’une seule fenêtre.

Soit vous créez une variable allouée dynamicment,

 void LIB_CreateWindow( ..... ) { ..... LIB_Window *newWnd = malloc( sizeof(LIB_Window) ); *newWnd = wind; return newWnd; } 

ou vous déclarez la variable de type LIB_Window dehors de la fonction et la transmettez à la fonction par un pointeur:

 void LIB_CreateWindow( LIB_Window *ptr_wnd, ..... ); int main( void ) { ..... LIB_Window wind; LIB_CreateWindow( &wnd, ..... ); ..... } 

Bien entendu, la libération en mémoire de la structure de données LIB_Window , dans la fonction LIB_DestroyWindow , ne fonctionne que si la mémoire de la variable a été allouée de manière dynamic:

 LIB_DestroyWindow() { ..... free(window); // <--- this only works if window was allocated by malloc }