Problem with glew.h

Started by
14 comments, last by MarkS_ 8 years, 7 months ago

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;
Advertisement

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

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.

Just don`t see, it`s work. Thanks alot for your help.

Just don`t see, it`s work.


I have no clue what you are trying to say here.

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.

This topic is closed to new replies.

Advertisement