求问这段代码里有哪里是没有正确释放的么???遇到了以下问题,google说是内存没有正确释放,但是我找不到错误
3DRFRvs.exe 中的 0x01196fc2 处最可能的异常: 0xC0000005: 读取位置 0xcdcdcdf5 时发生访问冲突#include "stdafx.h"
#include "OpenGL.h"
#include <string>
using namespace std;// #include "..\\DataStruct.h"
// #include "..\\BaseClass\\DataPreProcess.h"
// #include "..\\3Dcode\\FaceData3D.h"
GLuint listName[200];COpenGL::COpenGL(HDC dc, GLsizei iWidth, GLsizei iHeight)
{
assert(iWidth>0 && iHeight>0);

// m_hDC = GetDC(  (HWND)parentForm->Handle.ToInt32()  );
if(dc)
{
InitOpenGL(dc,iWidth,iHeight);
}
}COpenGL::~COpenGL(void)
{  
if (wglGetCurrentContext() != NULL) {
wglMakeCurrent(NULL,  NULL);
}
if(m_hRC){
wglDeleteContext(m_hRC);
m_hRC = NULL;
}
}bool COpenGL::InitOpenGL(HDC dc, int iWidth, int iHeight) //此处开始对OpenGL进行所有设置
{
if (dc == NULL){
string msg = "无效的DC";
return false;
}
m_hDC = dc;
m_dNearPlane   = 10; 
m_dFarPlane    = 10000.0; 
m_width = iWidth;
m_height = iHeight;
// 1 设置像素格式  [6/6/2012 Administrator]
MySetPixelFormat(dc,32,32); // 2 创建 RC
if (!(m_hRC=wglCreateContext(dc))) // 
{
string msg = "wglCreateContext Failed";
return false;
}
// 3 全局显示设置
SetRc();
SpecialEffect(); // 4 设置投影及视口
SetProjection(iWidth,iHeight,PERSPECTIVE); // 默认是perspective
ClearRc(); return true;
}
bool COpenGL::MySetPixelFormat(HDC hdc,int ColorBits,int DepthBits)
{   
GLuint PixelFormat;//保存查找匹配的像素格式结果 PIXELFORMATDESCRIPTOR pfd= // /pfd 告诉窗口我们所希望的东东,即窗口使用的像素格式
{
sizeof(PIXELFORMATDESCRIPTOR), // 上述格式描述符的大小
1, // 版本号
PFD_DRAW_TO_WINDOW | // 格式支持窗口
PFD_SUPPORT_OPENGL | // 格式必须支持OpenGL
PFD_DOUBLEBUFFER, // 必须支持双缓冲
PFD_TYPE_RGBA, // 申请 RGBA 格式
ColorBits, // 选定色彩深度
0, 0, 0, 0, 0, 0, // 忽略的色彩位
0, // 无Alpha缓存
0, // 忽略Shift Bit
8, // 无累加缓存
0, 0, 0, 0, // 忽略聚集位
DepthBits, // 16位 Z-缓存 (深度缓存)
0, // 无蒙板缓存
0, // 无辅助缓存
PFD_MAIN_PLANE, // 主绘图层
0, // Reserved
0, 0, 0 // 忽略层遮罩
}; if (!(PixelFormat=ChoosePixelFormat(hdc,&pfd))) // Windows 找到相应的象素格式了吗?
{
string msg = "ChoosePixelFormat Failed";
return FALSE; // 返回 FALSE
}
if(!SetPixelFormat(hdc,PixelFormat,&pfd)) // 能够设置象素格式么?
{
string msg = "SetPixelFormat Failed";
return FALSE; // 返回 FALSE
}/* gx
if(!wglMakeCurrent(m_hDC,m_hRC)) // 尝试激活着色描述表
{
MessageBox::Show("wglMakeCurrent Failed");
return FALSE; // 返回 FALSE
}
*/
return true;
}void COpenGL::SpecialEffect()
{
glShadeModel(GL_SMOOTH);//启用阴影平滑
glClearDepth(1.0f);//设置深度缓存

glEnable(GL_DEPTH_TEST);//启用深度测试
glDepthFunc(GL_LEQUAL);//所作深度测试的类型 glEnable(GL_POINT_SMOOTH);//启用平滑 glFrontFace(GL_CCW);
glEnable(GL_CULL_FACE);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);//告诉系统对透视进行修正
// glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
/*
glShadeModel(GL_SMOOTH);//启用阴影平滑
glClearColor(0.0f,0.0f,0.0f,0.0f);//黑色背景
glClearDepth(1.0f);//设置深度缓存
glEnable(GL_DEPTH_TEST);//启用深度测试
glDepthFunc(GL_LEQUAL);//所作深度测试的类型
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);//告诉系统对透视进行修正
glMatrixMode(GL_PROJECTION); //选择投影矩阵
*/}
void COpenGL::SetRc()
{
if (!wglMakeCurrent(m_hDC,m_hRC)){
string msg = "wglMakeCurrent Failed!";
}
}void COpenGL::ClearRc()
{
wglMakeCurrent(NULL,NULL);
}/***********************************************************  投影及视口设置 [6/6/2012 Administrator]
  
***********************************************************/
void COpenGL::SetProjection(int iWidth, int iHeight, ProType pers)
{ // 1 投影
if (pers == PERSPECTIVE) {
GLfloat fAspect;
if (iHeight > 0)
fAspect = (GLfloat)iWidth / iHeight;
else
fAspect = 1.0f;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0f, fAspect, m_dNearPlane, m_dFarPlane);
glMatrixMode(GL_MODELVIEW);
}else {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, iWidth, 0.0, iHeight, -m_dFarPlane, m_dFarPlane);
//  glOrtho(-75.0,75.0,-75.0*(GLfloat)iHeight/(GLfloat)iWidth,
//  75.0*(GLfloat)iHeight /(GLfloat)iWidth,-100,100.0);
glMatrixMode(GL_MODELVIEW);
}
// 2 视口
glViewport(0, 0, iWidth, iHeight);

}
//////////////////////////////////////////////////////////////////////////
// 下面非gx封装void COpenGL::SwapOpenGLBuffers(void)
{
SwapBuffers(m_hDC) ;
}void COpenGL::EnableLight()
{
SetRc();
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
//  glEnable(GL_LIGHT1);
glEnable(GL_NORMALIZE);
ClearRc();
}void COpenGL::DisnableLight()
{
SetRc();
glDisable(GL_COLOR_MATERIAL);
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
//  glDisable(GL_LIGHT1);
glDisable(GL_NORMALIZE);
ClearRc();
}

解决方案 »

  1.   

    补充下,出错的时候箭头指在这个地方的if那里void COpenGL::SetRc()
    {
    if (!wglMakeCurrent(m_hDC,m_hRC)){
    string msg = "wglMakeCurrent Failed!";
    }
    }
      

  2.   

    看代码没有发现明显错误。。单步调试看看 m_hDC,m_hRC 的值
      

  3.   

    我在其他帖子(http://www.uipower.com/bbs/viewthread.php?tid=204662&extra=&page=2)看到个类似的错误是不是dc的问题啊我把view截过来了,这里的dc是view里获取的。但是我对mfc的显示机制不是很懂
    // 3DRFRvsView.cpp : CMy3DRFRvsView 类的实现
    //#include "stdafx.h"
    #include "3DRFRvs.h"#include "3DRFRvsDoc.h"
    #include "3DRFRvsView.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    // CMy3DRFRvsViewIMPLEMENT_DYNCREATE(CMy3DRFRvsView, CScrollView)BEGIN_MESSAGE_MAP(CMy3DRFRvsView, CScrollView)
    ON_WM_STYLECHANGED()
    ON_WM_SIZE()
    ON_WM_LBUTTONDOWN()
    ON_WM_LBUTTONUP()
    ON_WM_MOUSEMOVE()
    END_MESSAGE_MAP()// CMy3DRFRvsView 构造/析构CMy3DRFRvsView::CMy3DRFRvsView()
    {
    // TODO: 在此处添加构造代码
    m_nMapMode = MM_TEXT;
    }CMy3DRFRvsView::~CMy3DRFRvsView()
    {
    }BOOL CMy3DRFRvsView::PreCreateWindow(CREATESTRUCT& cs)
    {
    // TODO: 在此处通过修改
    //  CREATESTRUCT cs 来修改窗口类或样式
    cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CS_OWNDC;
    return CScrollView::PreCreateWindow(cs);
    }void CMy3DRFRvsView::OnInitialUpdate()
    {
    CScrollView::OnInitialUpdate();
    // TODO: 调用 GetListCtrl() 直接访问 ListView 的列表控件,
    //  从而可以用项填充 ListView。
    HDC dc = ::GetDC(this->m_hWnd);
    // HDC tc= pDC->m_hDC;
    // m_dc = new CClientDC(this);
    m_pDraw = GetDocument()->GetRightDrawObj(); 
    m_pDraw->SetDrawType(GetDocument()->m_drawType);
    CRect Rect;
    GetClientRect(&Rect);
    m_pDraw->InitSetting(/*m_dc->GetSafeHdc()*/dc,Rect.Width(),Rect.Height());
    }void CMy3DRFRvsView::OnRButtonUp(UINT nFlags, CPoint point)
    {
    ClientToScreen(&point);
    OnContextMenu(this, point);
    }void CMy3DRFRvsView::OnContextMenu(CWnd* pWnd, CPoint point)
    {
    theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
    }
    // CMy3DRFRvsView 诊断#ifdef _DEBUG
    void CMy3DRFRvsView::AssertValid() const
    {
    CScrollView::AssertValid();
    }void CMy3DRFRvsView::Dump(CDumpContext& dc) const
    {
    CScrollView::Dump(dc);
    }CMy3DRFRvsDoc* CMy3DRFRvsView::GetDocument() const // 非调试版本是内联的
    {
    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy3DRFRvsDoc)));
    return (CMy3DRFRvsDoc*)m_pDocument;
    }
    #endif //_DEBUG
    // CMy3DRFRvsView 消息处理程序
    void CMy3DRFRvsView::OnStyleChanged(int nStyleType, LPSTYLESTRUCT lpStyleStruct)
    {
    //TODO: 添加代码以响应用户对窗口视图样式的更改
    CScrollView::OnStyleChanged(nStyleType,lpStyleStruct);
    }void CMy3DRFRvsView::OnDraw(CDC* pDC)
    {
    if (m_pDraw != NULL){
    m_pDraw->SetLight();
    m_pDraw->DoDrawing();
    }
    // 绘制激活窗口边框
    if (GetDocument()->m_activeWnd == 1){
    CBrush brush;
    brush.CreateSolidBrush(RGB(255,255,0));
    CRect Rect;
    GetClientRect(&Rect);
    pDC->FrameRect(&Rect, &brush);
    }
    }void CMy3DRFRvsView::OnSize(UINT nType, int cx, int cy)
    {
    CScrollView::OnSize(nType, cx, cy); // TODO: 在此处添加消息处理程序代码 m_pDraw->ResizeWindow(cx,cy);
    Invalidate();
    }void CMy3DRFRvsView::OnLButtonDown(UINT nFlags, CPoint point)
    {
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    if (GetDocument()->SetActiveWnd(1)){
    CClientDC dc(this);
    OnDraw(&dc);
    GetDocument()->UpdateAllViews(this);
    }
    CScrollView::OnLButtonDown(nFlags, point);
    }void CMy3DRFRvsView::OnLButtonUp(UINT nFlags, CPoint point)
    {
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    m_pDraw->ClearMousePos(); CScrollView::OnLButtonUp(nFlags, point);
    }void CMy3DRFRvsView::OnMouseMove(UINT nFlags, CPoint point)
    {
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    if ( nFlags & MK_LBUTTON || nFlags & MK_RBUTTON )
    {
    if( nFlags & MK_SHIFT ) {
    // SHIFT is being pressed. Zoom the camera.
    m_pDraw->SetScale(point.x,point.y);
    }else if ( nFlags & MK_CONTROL ){
    m_pDraw->SetTranslation(point.x,point.y);
    }else{
    m_pDraw->SetRotation(point.x,point.y);
    }
    //
    CClientDC dc(this);
    OnDraw(&dc);
    }
    CScrollView::OnMouseMove(nFlags, point);
    }
      

  4.   


    我被我自己折服了去winmain里单步了,错误在这里的第一个if // Perform specific initializations
    if (!pThread->InitInstance())
    {
    if (pThread->m_pMainWnd != NULL)
    {
    TRACE(traceAppMsg, 0, "Warning: Destroying non-NULL m_pMainWnd\n");
    pThread->m_pMainWnd->DestroyWindow();
    }
    nReturnCode = pThread->ExitInstance();
    goto InitFailure;
    }
    nReturnCode = pThread->Run();