SwapBuffers Access Violation Topic is solved

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
mach5mach
In need of some credit
In need of some credit
Posts: 6
Joined: Mon Apr 21, 2008 5:25 pm

SwapBuffers Access Violation

Post by mach5mach » Mon Apr 21, 2008 9:48 pm

I'm trying to use the following code but I can't get past the SwapBuffer call. The break takes me to the glcanvas.cpp and the statement:
::SwapBuffers((HDC) m_hDC);
and it says that there is an access violation.
I've gone through and checked to make sure that I have my PaintDC set up and the GLContext set up but I can't figure out why this is happening. I'm leaning towards it has to do with OpenGL. I tried one of the examples and it broke on the swapbuffer call too. Is there something I need to build so that the m_hDC is defined? Really stuck here. If it helps I'm running Vista 64 bit with SP1 installed and using VC 2008.

Code: Select all

GLCanvas::GLCanvas(wxWindow* parent, int* attribList,  const wxPoint& pos, const wxSize& size)
:wxGLCanvas(parent, -1, attribList, pos, size){
	m_init = 0;
	m_MainGLContext = new wxGLContext(this);

};

void GLCanvas::OnPaint(wxPaintEvent& WXUNUSED(event)){
if( !m_MainGLContext){ 
      return; 
    }
	SetCurrent(*m_MainGLContext);	//set before opengl commands
	wxPaintDC(this);
	if(!m_init){
		InitGL();
	}
	
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

	Draw();

	glFlush();
	SwapBuffers();
}

Deschamps
Knows some wx things
Knows some wx things
Posts: 30
Joined: Tue May 01, 2007 12:16 pm

Re: SwapBuffers Access Violation

Post by Deschamps » Tue Apr 22, 2008 9:06 am

mach5mach wrote:

Code: Select all

	(...)
	SetCurrent(*m_MainGLContext);	//set before opengl commands
	(...)
I'm not sure about this, but... why are you calling SetCurrent in this manner (with a pointer to a pointer)? I suppose that you should use: m_MainGLContext->SetCurrent(this) or his equivalent form this->SetCurrent( m_MainGLContext ), assuming you used the fourth constructor for your wxGLCanvas object.

Regards.
Misquotations are the only quotations that are never misquoted

mach5mach
In need of some credit
In need of some credit
Posts: 6
Joined: Mon Apr 21, 2008 5:25 pm

Post by mach5mach » Tue Apr 22, 2008 2:21 pm

here is the header file for the GLCanvas.h. I just started working with wxWidgets so let me know if I'm making a bad decision in my setup. Yes I am trying to use the 4th constructor although I'm beginning to regret it. I also tried changing my context variable from a pointer to an instance but then I got compiler errors for no default constructor for wxGLContext

Code: Select all

#ifndef _GLCANVASH_
#define _GLCANVASH_

#include "wx\glcanvas.h"
#include "wx\dcclient.h"
#include "gl\GL.h"
#include "gl\GLU.h"
#include <list>
using namespace std;

class GLCanvas: public wxGLCanvas
{
public:

	GLCanvas::GLCanvas(wxWindow* parent, int* attribList = 0,  const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize);
	void GLCanvas::OnLeftClick(wxMouseEvent& WXUNUSED(event));
	void GLCanvas::OnPaint(wxPaintEvent& WXUNUSED(event));
	void GLCanvas::Draw();
	void GLCanvas::OnEraseBackground(wxEraseEvent& WXUNUSED(event));
	void GLCanvas::OnSize(wxSizeEvent& event);
	wxGLContext GLCanvas::GetGLContext();

private:
	int m_init;
	wxGLContext* m_MainGLContext;
	list<wxPoint>* m_PointList;

	void GLCanvas::InitGL();

    DECLARE_EVENT_TABLE()
};

#endif
I tried your suggestion but I got compiling errors when I don't include the pointer in the setcurrent call.


cannot convert parameter 1 from 'GLCanvas *const ' to 'const wxGLCanvas &'

or the other way

cannot convert parameter 1 from 'wxGLContext *' to 'const wxGLContext &'

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria » Tue Apr 22, 2008 5:37 pm

You may need to learn more about C++ operators * and &

mach5mach
In need of some credit
In need of some credit
Posts: 6
Joined: Mon Apr 21, 2008 5:25 pm

Post by mach5mach » Tue Apr 22, 2008 7:29 pm

Yes, I definitely still haven't gotten a good grasp on pointers and references but I don't think that's the issue here. I've tried running some of the provided samples, i.e. isosurf, and they also crash at the swapbuffers statement. I don't know if it's because I didn't build a library correctly or what.

Is there a better way to set up the GLCanvas class so that it can hold a context?

Deschamps
Knows some wx things
Knows some wx things
Posts: 30
Joined: Tue May 01, 2007 12:16 pm

Post by Deschamps » Tue Apr 22, 2008 7:50 pm

mach5mach wrote:Is there a better way to set up the GLCanvas class so that it can hold a context?
Well. I admit that i had never used the 4th constructor for wxGLCanvas (I'm a creature of habit and always used the first one). But, I've tested updating a piece of code I had created time ago, and it has worked fine using the fourth constructor, a wxGLContext and the SetCurrent form you've been using (see attached picture).

I've also included the source code, if you want to use it as reference. (I would suggest you to have a look also to the class definitions style).

Regards.
Attachments
example.png
Sample screenshot under Gnome (wxGTK)
example.png (17.56 KiB) Viewed 2222 times
hidding_panels.tar.gz
Source code (including C::B project)
(3.66 KiB) Downloaded 78 times
Misquotations are the only quotations that are never misquoted

woo472
In need of some credit
In need of some credit
Posts: 1
Joined: Thu Apr 17, 2008 5:30 pm
Location: Norfolk UK

Post by woo472 » Tue Apr 22, 2008 8:52 pm

The context creation is correct.

wxGLContext* context1;
context1 = new wxGLContext(this);
SetCurrent(*context1);
The violation is called by the wrong swapbuffer call (windows)

Preceed the call with the Canvas name

Myglframe->Swapbuffers(); or whatever you call it

I also noted a call to GetGLContext this always returns NULL with the fourth contructor.

I do have a 'working' example of this, it has three screens with a teapot front view, right and top If you think this may help
I am so old I used to program before compilers were thought of.

mach5mach
In need of some credit
In need of some credit
Posts: 6
Joined: Mon Apr 21, 2008 5:25 pm

Post by mach5mach » Tue Apr 22, 2008 10:39 pm

I think woo is on the right track. When I use a swapbuffer call in my Frame class that controls the Canvas class such as:
Frame->GLCanvas->SwapBuffers()
it doesn't break. However, I can't figure out a way to get the swapbuffer call to work inside my GLCanvas class. I saw in Deschamps and in other people's code that they just have a call to SwapBuffer in the OnPaint event or another Draw procedure. I'd like to be able to use the swapbuffer call inside my GLCanvas class so that it can be used in Idle events and other such events. I've tried specifying the namespace of the call and using this-> in front of it but to no avail.

mach5mach
In need of some credit
In need of some credit
Posts: 6
Joined: Mon Apr 21, 2008 5:25 pm

Post by mach5mach » Wed Apr 23, 2008 2:26 am

I'm going to attach a little more of my code and maybe it'll help in finding the problem
Attachments
GLCanvas.cpp
(1.28 KiB) Downloaded 77 times
GLCanvas.h
(717 Bytes) Downloaded 45 times
MainFrame.cpp
(1.8 KiB) Downloaded 49 times

mach5mach
In need of some credit
In need of some credit
Posts: 6
Joined: Mon Apr 21, 2008 5:25 pm

Post by mach5mach » Thu May 01, 2008 7:45 pm

I found the problem. When I turn my second graphics card off, then my program works, and swap buffers performs correctly. So the problem should have something to do with the CrossfireX or how I connected the two cards or something. Thanks for all the help anyway.

Post Reply