无法找到内存泄漏

时间:2013-11-16 03:22:23

标签: c++ memory-leaks heap glut

我的任务是建立一个光线追踪程序来计算光线的影响 世界各种物体。 基本上,该计划如下:

for each pixel P
    calculate a ray from the camera location to the current far pixel projection
        for each shape in queue shapeList
            if the ray intersects that shape
                for each world lightsource m
                      calculate that intersect location's respective pixel color

它确实有效,但最终消耗了我的笔记本电脑的整个内存池,我无法弄清楚原因。我的笔记本电脑有6台内存,所以没有充分的理由。

当我输入以下代码段时,特别会发生内存泄漏:这是我评估模拟二次公式以测试光线交叉点的函数的结果。

我确信在这个块中发生了泄漏,因为第二个条件被触发,我的内存使用量猛增并且很快超过了可用的物理RAM。

            else if (result[2]==2)
            {
                //compute 3d vertices of each hit:
                GLdouble hitX0 = Peye.x + (dir.x * result[0]);
                GLdouble hitY0 = Peye.y + (dir.y * result[0]);
                GLdouble hitZ0 = Peye.z + (dir.z * result[0]);
                Vertex hitA = Vertex(hitX0, hitY0, hitZ0);

                GLdouble hitX1 = Peye.x + (dir.x * result[1]);
                GLdouble hitY1 = Peye.y + (dir.y * result[1]);
                GLdouble hitZ1 = Peye.z + (dir.z * result[1]);
                Vertex hitB = Vertex(hitX1, hitY1, hitZ1);

                Vertex curShapePos = Vertex(0.0, 0.0, 0.0); //init @ origin
                GLdouble **shapeCurCTM = it->getCTM();
                GLdouble *tempShapePos = vectorMatrixMult(curShapePos, shapeCurCTM); //multiply above position by current shape's CTM to achieve its current position
                curShapePos.x = tempShapePos[0];
                curShapePos.y = tempShapePos[1];
                curShapePos.z = tempShapePos[2];

                for (int i = 0; i < 4; i++)
                {
                    delete[] shapeCurCTM[i];
                }
                delete[] shapeCurCTM;
                delete[] tempShapePos;

                //////////////////calculate lighting values for current intersect point////////////////////////////////////////////////////////////
                GLdouble finalIntensity_R = 0.0; 
                GLdouble finalIntensity_G = 0.0;
                GLdouble finalIntensity_B = 0.0; //final intensity for wavelength LAMBDA
                GLdouble ambR = 0.0;
                GLdouble ambG = 0.0;
                GLdouble ambB = 0.0;
                GLdouble diffR = 0.0;
                GLdouble diffG = 0.0;
                GLdouble diffB = 0.0;

                vec3 N = vec3(hitA, curShapePos); 
                vec3 *Nnorm = N.normalize(); //the UNIT LENGTH surface normal at the point of intersection
                vec3 **L = new vec3*[lightList.size()]; //dynamically allocated array of vec3 objects, each of which will be subsequently allocated on the heap by calling normalize (see below)
                //std::deque<Light>::iterator m;
                for (std::deque<Light>::iterator m = lightList.begin(); m != lightList.end(); m++) //sum values for all lights m <-> the set of all lights M, where M = {m0, m1, ..., mn}
                {
                    //compound rgb values for each light parameter...for usage in the intensity equation...
                    ambR+=m->amb[0];
                    ambG+=m->amb[1];
                    ambB+=m->amb[2];

                    diffR+=m->diff[0];
                    diffG+=m->diff[1];
                    diffB+=m->diff[2];

                    L[std::distance(lightList.begin(), m)] = vec3(m->position, hitA).normalize(); //each implicit entry within L contains a normalized vector allocated ON THE HEAP due to the call to normalize()
                }

                GLdouble globalAmbient[3] = {ambR, ambG, ambB}; //global intensity of ambient light...aka sum all ambient light RGB values...
                GLdouble objAmbient[4] = {it->amb[0], it->amb[1], it->amb[2], it->amb[3]}; //object's ambient color for wavelength LAMBDA; in our case the object's R, G or B value for ambient color...
                GLdouble mTotalIntensity[3] = {(ambR+diffR), (ambG+diffG), (ambB+diffB)}; //intensity of light m for wavelength LAMBDA; in our case the R, G, or B value of the light color for light m
                GLdouble globalDiffuse[3] = {diffR, diffG, diffB}; //global diffuse coefficient
                GLdouble objDiffuse[3] = {it->diff[0], it->diff[1], it->diff[2]}; //object's diffuse color wave length LAMBDA; in our case the OBJECT's R, G or B value for diffuse color.

                for (int i = 0; i < lightList.size(); i++) //for each ith light in the world, compound the respective RGB values...
                {   
                    finalIntensity_R += ((globalAmbient[0]*it->amb[0]) + ((mTotalIntensity[0] * (globalDiffuse[0] * it->diff[0] * Nnorm->dot(*L[i])))));
                    finalIntensity_G += ((globalAmbient[1]*it->amb[1]) + ((mTotalIntensity[1] * (globalDiffuse[1] * it->diff[1] * Nnorm->dot(*L[i])))));
                    finalIntensity_B += ((globalAmbient[2]*it->amb[2]) + ((mTotalIntensity[2] * (globalDiffuse[2] * it->diff[2] * Nnorm->dot(*L[i])))));
                }

                GLdouble LAMBDA[3] = {finalIntensity_R, finalIntensity_G, finalIntensity_B};
                ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                //std::cout << "SPHERE: TWO HITS DETECTED" << std::endl;
                printf("2 hits detected. LAMBDA for Shape #%d = {%g, %g, %g}. Raster_x = %d, Raster_y = %d\n", (int)std::distance(shapeList.begin(), it), LAMBDA[0], LAMBDA[1], LAMBDA[2], raster_x, raster_y);
                ////////////////////////draw pixel/////////////////////////////////////////////////////////////////////////////
                //plotPixel(screen, raster_x, raster_y, 1.0f, 0.0f, 0.0f, 1.0f);
                plotPixel(screen, raster_x, raster_y, LAMBDA[0], LAMBDA[1], LAMBDA[2], 1.0f);
                glGenTextures(1, &texId);
                glBindTexture(GL_TEXTURE_2D, texId);
                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, WINDOW_WIDTH, WINDOW_HEIGHT, 0, GL_RGBA, GL_FLOAT, (void*)screen);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
                /////////////////////////////////////////////////////////////////////////////////////////////////////////////

                for (int i = 0; i < lightList.size(); i++)
                {
                    delete L[i]; //delete each DYNAMICALLY ALLOCATED vec3 object resultant from the explicit call to each vec3's normalize() method contained within L[].
                }
                delete[] L;
                delete Nnorm;
            }

std::deque<Shape>::iterator

我知道至少可以说这是一个有点模糊的问题,但我在Visual Studio 2012尝试了几种内存泄漏分析工具,并且在确定原点时尚未取得任何成功。

感谢您的帮助,如果需要,我将非常乐意提供任何其他详细信息。

0 个答案:

没有答案