0xC0000005:访问冲突读取位置0x00000000C284EFA2

0xC0000005:访问冲突读取位置0x00000000C284EFA2,c,opengl,memory,C,Opengl,Memory,我要处理的项目利用了openGL、C和Tcltk。我目前正在使用Tcl内存跟踪工具调试内存泄漏。以下是导致错误的代码片段: //there is a check here to make sure context isn't null i.e. if(context != NULL) glRotatef(context[model_index].rotation[0], 1, 0, 0); //error occurs here, sometimes varies between each gl

我要处理的项目利用了openGL、C和Tcltk。我目前正在使用Tcl内存跟踪工具调试内存泄漏。以下是导致错误的代码片段:

//there is a check here to make sure context isn't null i.e. if(context != NULL)
glRotatef(context[model_index].rotation[0], 1, 0, 0); //error occurs here, sometimes varies between each
glRotatef(context[model_index].rotation[1], 0, 1, 0);
glRotatef(context[model_index].rotation[2], 0, 0, 1);
错误输出:

Exception thrown at 0x00007FFFF1C9AB21 (tkogl2.dll) in rsession.exe: 0xC0000005: Access violation reading location 0x00000000C284EFA2
下面是一些额外的代码,以提供一些关于什么是
上下文以及如何使用它的参考:

//struct definition and intilization
typedef struct {
    float x;
    float y;
    float z;
    float scale;
    float rotation[3];
} context_t;
context_t* context = NULL;

//memory allocation
if (context != NULL)
{
    ckfree((char*)context);
    context = NULL;
}
if (amount > 0) 
{
    context = (context_t*)ckalloc(amount*sizeof(context_t));
}
我认为可能是由于某种原因,
model_index
太大,超出了范围,但将其固定为
0
仍然会导致相同的错误。如果
上下文!=NULL
是检查上下文是否确实存在的正确方法,那么在执行时它不应该是NULL。我缺乏知识去寻求可能导致这种情况的任何其他选择,因此我希望能够深入了解我可以研究的潜在途径,以便更详细地探讨这个问题

下面是一些为
上下文的属性赋值的代码:

//rotations
const char* attr = Tcl_GetStringFromObj(objv[1], NULL);
    if (strcmp(attr, "angle") == 0)
    {
        if (model_amount == 0)
        {
            return TCL_OK;
        }
        const char* d = Tcl_GetStringFromObj(objv[2], NULL);
        double r;
        Tcl_GetDoubleFromObj(interp, objv[3], &r);
        /*apply rotations*/
        switch (d[0]) {
        case 'x':
            context[model_index].rotation[0] += (float)r;
            ANGLE_REDUCE(context[model_index].rotation[0]);
            break;
        case 'y':
            context[model_index].rotation[1] += (float)r;
            ANGLE_REDUCE(context[model_index].rotation[1]);
            break;
        case 'z':
            context[model_index].rotation[2] += (float)r;
            ANGLE_REDUCE(context[model_index].rotation[2]);
            break;
        }
        char* msg = ckalloc(512);
        sprintf(msg, "Rotate: %f %f %f",
            context[model_index].rotation[0],
            context[model_index].rotation[1],
            context[model_index].rotation[2]);
        Tcl_SetResult(interp, msg, TCL_DYNAMIC);

        Tcl_ValidateAllMemory(__FILE__, __LINE__);
    }

//scaling
if (model_amount == 0)
        {
            return TCL_OK;
        }

        const char* value = Tcl_GetStringFromObj(objv[2], NULL);
        /*apply scaling*/
        if (strcmp(value, "in") == 0)
        {
            context[model_index].scale += 0.1;
        }
        else if (strcmp(value, "out") == 0 && context[model_index].scale > 0.1)
        {
            context[model_index].scale -= 0.1;
        }

它似乎还会生成错误
0xffffffffffffff

在您使用
ckalloc
保留内存的那一刻和调用
dglRotatef()
之间会发生什么?您确定上下文尚未被破坏吗?将为x、y、z坐标以及缩放和旋转指定值。我还将添加一些片段。我看到崩溃发生在
tkogl2.dll
中。您确定以与dll相同的模式生成程序吗?为了避免这种情况:调试中的程序,在发行版中加载Dll。我相信是这样吗?我有“调试x64”,然后在VS2019中使用“本地Windows调试器”,并将
tkogl2.dll
添加到调用它的R库中。我是如何在Visual Studio中启动我的R程序并连接到进程,然后以这种方式进行测试的。您可以通过以下方式连接到进程: