0

The project I was given to mess with utilizes openGL, C, and Tcltk. I am currently debugging a memory leak using Tcl memory tracing tools. Here is the snippet of code that is causing the error:

//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);

Error output:

Exception thrown at 0x00007FFFF1C9AB21 (tkogl2.dll) in rsession.exe: 0xC0000005: Access violation reading location 0x00000000C284EFA2

Here is some additional code to give some reference for exactly what context is and how it is being used:

//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));
}

I thought perhaps model_index was for some reason too large and making it go out of bounds, but fixing it to 0 still caused the same error. If context != NULL is a proper way to check that context actually exists, then it shouldn't be NULL when executing. I lack the knowledge to pursue any additional options that could be causing this, so I hope to gain insight on potential avenues I could look at to explore this issue in more detail.

Here are some pieces of code that assign values to attributes of context:

//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;
        }

It appears it also generates the error 0xffffffffffffffff

hkj447
  • 677
  • 7
  • 21
  • what happens between the moment you reserve the memory with `ckalloc` and the call of `dglRotatef ()` ? are you sure the context has not already been destroyed? – Landstalker Mar 05 '20 at 15:58
  • Values are assigned to x,y,z coords as well as scale and rotation. I will add some snippets of that as well. – hkj447 Mar 05 '20 at 16:03
  • I see that the crash occurs in `tkogl2.dll`. Are you sure that you generate your program in the same mode as your dll? to avoid the case: program in Debug, load Dll in release. – Landstalker Mar 05 '20 at 16:04
  • I believe so? I have "Debug x64" and then use "Local Windows Debugger" in VS2019 and add `tkogl2.dll` to an R library which calls it. How I test it though is booting up my R program and attach to process in Visual Studio, then go through the motions that way. – hkj447 Mar 05 '20 at 16:08
  • you can attach to process by this : https://stackoverflow.com/questions/60541684/how-to-attach-a-process-to-the-current-debugger-programmatically/60541886?noredirect=1#comment107105568_60541886 – Landstalker Mar 05 '20 at 16:11
  • you are sure that your `model_index` never exceeds the value of `amount` with which the buffer was reserved with `ckalloc`. Unfortunately, we cannot verify this information from the posted code – Landstalker Mar 05 '20 at 16:17
  • Yes, I also tried simply changing `context[model_index]` to `context[0]` and the same error was give. – hkj447 Mar 05 '20 at 16:20
  • Have you checked whether the reservation with `ckalloc` has been successfully executed and returns a valid pointer for `context`? – Landstalker Mar 05 '20 at 16:24
  • Does that equate to checking if it is `NULL`? – hkj447 Mar 05 '20 at 16:26
  • Yes, may be you have a very big `amount` number. – Landstalker Mar 05 '20 at 16:32

0 Answers0