Sign in to follow this  
Valera

Problem with glew.h

Recommended Posts

Hi all, I try use glew. This my code

if (glewInit() == GLEW_NO_ERROR)
    {
      GLuint vertexbuffer;
      glGenBuffers(1, &vertexbuffer);
    }

and I get an error on glGenBuffers An unhandled exception of type 'System.AccessViolationException' occurred in ManagedLib.dll, in compiled my dll c++/cli

Share this post


Link to post
Share on other sites

I create context on event form_load 

GLManagedWrapper::GLManagedWrapper(INT_PTR hWnd)
	{

		if (!glewInit()) return;
		glewExperimental = GL_TRUE;


		m_Hwnd = (HWND)hWnd;
		if (!m_Hwnd) return;

		int m_Width, m_Height;
		LPRECT rect = (LPRECT)malloc(sizeof(RECT));
		if (GetWindowRect(m_Hwnd, rect))
		{
			m_Width = rect->right - rect->left;
			m_Height = rect->bottom - rect->top;
		}
		else return;

		free(rect);

		m_Hdc = GetDC(m_Hwnd);

		if (m_Hdc)
		{
			GLSetPixelFormat(m_Hdc);
			ReSizeGLScene(m_Width, m_Height);
			InitGL();
		}
	}

Share this post


Link to post
Share on other sites

I try minimal example and have error 

#include "TestGlew.h"

using namespace TestTest;

void Test1::Test()
{
	glewExperimental = GL_TRUE;
	glewInit();
	GLuint vertexBuffer;
	glGenBuffers(1, &vertexBuffer);
	printf("%u\n", vertexBuffer);
}

and C# call dll

var t = new Test1();
            t.Test();

Share this post


Link to post
Share on other sites

As you are using C# i assume your OS is Windows?

 

I had a similary error once, where everything worked fine until i tried to create the first buffer in OpenGL.

 

Unfortunately i don't remember the excact solution an have currently no acces to my code, but i believe it was some wrong header/header order.

Edited by mgubisch

Share this post


Link to post
Share on other sites

I've found some other code which words for me on windows: IIRC the crash was somewhere deep in the driver.

 

I included my headers the following order:

 

#include <windows.h>
#include <glew.h>
#include <wglew.h>

 

this is how i initialized glew and OpenGL

	m_hDC = GetDC(m_hWnd);
	m_hGlrc = wglCreateContext(m_hDC);
	wglMakeCurrent(m_hDC, m_hGlrc);
	glewInit();
        wglMakeCurrent(m_hDC, m_hGlrc);

Hope you can find there what you are missing

 

Share this post


Link to post
Share on other sites

Create the context before calling glewInit.  Your code:

GLManagedWrapper::GLManagedWrapper(INT_PTR hWnd)
	{

		if (!glewInit()) return;
		glewExperimental = GL_TRUE;


		m_Hwnd = (HWND)hWnd;
		if (!m_Hwnd) return;

		int m_Width, m_Height;
		LPRECT rect = (LPRECT)malloc(sizeof(RECT));
		if (GetWindowRect(m_Hwnd, rect))
		{
			m_Width = rect->right - rect->left;
			m_Height = rect->bottom - rect->top;
		}
		else return;

		free(rect);

		m_Hdc = GetDC(m_Hwnd);

		if (m_Hdc)
		{
			GLSetPixelFormat(m_Hdc);
			ReSizeGLScene(m_Width, m_Height);
			InitGL();
		}
	}

This will never work because you're calling glewInit before you create your context.  You need to do it the other way around - move your glewInit call to after your InitGL call and it will work.  You also need to set glewExperimental before calling glewInit too.

Share this post


Link to post
Share on other sites

The main question has already been answered, but...

It is good that you called "free", but this is an unnecessary heap allocation that could lead to a memory fragmentation leak if you forget. Don't use the heap for stuff like this:
 

		int m_Width, m_Height;
		LPRECT rect = (LPRECT)malloc(sizeof(RECT)); // <- Why are you doing this?
		if (GetWindowRect(m_Hwnd, rect))
		{
			m_Width = rect->right - rect->left;
			m_Height = rect->bottom - rect->top;
		}
		else return; // Guaranteed memory leak if the function returns here. Where is the call to free?

		free(rect); // <- ??

Also, why are you using malloc/free instead of new/delete?

In this case, the RECT is allocated on the stack and is automatically deallocated when the function goes out of scope:
 

		int m_Width, m_Height;
		RECT rect;
		if (GetWindowRect(m_Hwnd,&rect))
		{
			m_Width = rect.right - rect.left;
			m_Height = rect.bottom - rect.top;
		}
		else return;
Edited by MarkS

Share this post


Link to post
Share on other sites

I use vs2013. compiling dll c++\cli and when i using LPRECT rect without malloc() this value becomes 0x00000000 and GetWindowRect() return false.

I am sorry, my english is bad rolleyes.gif

Edited by Valera

Share this post


Link to post
Share on other sites
LPRECT is just a typedef for a RECT*. You need to do something like this:
RECT rect;
if (GetWindowRect(m_Hwnd, &rect))
{
	m_Width = rect.right - rect.left;
	m_Height = rect.bottom - rect.top;
}

// rect is now freed automatically
// you never leak memory, you never need to manually delete it.

Share this post


Link to post
Share on other sites

I am sorry, my english is bad rolleyes.gif


It is important that you understand what we are telling you. Using malloc in this case is unnecessary and potentially dangerous. What is your primary language? Maybe someone can translate.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this