Embarcadero / Dev-Cpp

A fast, portable, simple, and free C/C++ IDE
https://www.embarcadero.com/free-tools/dev-cpp
GNU General Public License v2.0
2.43k stars 266 forks source link

debugger gets stuck at function referenced by wglGetProcAddress #219

Open IPEfluencer opened 2 years ago

IPEfluencer commented 2 years ago

When OpenGL extensions are used that have been referenced by wglGetProcAddress, the debugger gets stuck when calling this extension, i.e. executing a single step with that extension is not possible:

grafik

As a possible workaround, I usually set a further breakpoint after the critical line (containing the extension) and press "Continue" to jump to that breakpoint.

The example code above unfortunately does need some preparation, you also need to use -lopengl32 -lgdi32 as a linker setting. The minimal working example is:

#define APIENTRY
#define APIENTRYP APIENTRY *
#include <gl/gl.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  // some init, needed for wglGetProcAddress
  WNDCLASSEX windowClass;
  ZeroMemory(&windowClass, sizeof(WNDCLASSEX));
  windowClass.cbSize        = sizeof(WNDCLASSEX);
  windowClass.style         = CS_OWNDC;
  windowClass.lpfnWndProc   = (WNDPROC)(DefWindowProc);
  windowClass.hInstance     = hInstance;
  windowClass.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE);
  windowClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  windowClass.lpszClassName = "OpenGLTest";
  RegisterClassEx(&windowClass);
  HWND hWnd = CreateWindowEx(WS_EX_APPWINDOW, "OpenGLTest", "Test Title", WS_OVERLAPPEDWINDOW, 0, 0, 640, 480, HWND_DESKTOP, 0, hInstance, NULL);
  HDC hDC = GetDC(hWnd);
  PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL, PFD_TYPE_RGBA, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 8, 0, PFD_MAIN_PLANE, 0, 0, 0, 0 };
  GLuint PixelFormat = ChoosePixelFormat(hDC, &pfd);
  SetPixelFormat(hDC, PixelFormat, &pfd);
  HGLRC hRC = wglCreateContext(hDC);
  wglMakeCurrent(hDC, hRC);

  // interesting part
  typedef void (APIENTRYP PFNGLGENERATEMIPMAPPROC) (GLenum target);
  PFNGLGENERATEMIPMAPPROC glGenerateMipmap = (PFNGLGENERATEMIPMAPPROC)wglGetProcAddress("glGenerateMipmap");
  if (glGenerateMipmap) {
    // watch: debug with single step not possible here
    glGenerateMipmap(GL_TEXTURE_2D);
  }

  // clean up
  wglMakeCurrent(hDC, 0);
  wglDeleteContext(hRC);
  ReleaseDC(hWnd, hDC);
  DestroyWindow(hWnd);
  UnregisterClass("OpenGLTest", hInstance);
  return 0;
}