Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 找不到内存泄漏_C++_Memory Leaks_Heap_Glut - Fatal编程技术网

C++ 找不到内存泄漏

C++ 找不到内存泄漏,c++,memory-leaks,heap,glut,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 fo

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

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;
            }
else if(结果[2]==2)
{
//计算每个命中的3d顶点:
GLX0=Peye.x+(dir.x*结果[0]);
GLdouble-hitY0=Peye.y+(dir.y*结果[0]);
GLdouble hitZ0=Peye.z+(dir.z*结果[0]);
顶点hitA=顶点(hitX0、hitY0、hitZ0);
GLX1=Peye.x+(dir.x*结果[1]);
GLdouble hitY1=Peye.y+(dir.y*结果[1]);
GLZ1=Peye.z+(dir.z*结果[1]);
顶点hitB=顶点(hitX1、hitY1、hitZ1);
顶点curShapePos=顶点(0.0,0.0,0.0);//init@origin
GLdouble**shapecurtm=it->getCTM();
GLdouble*tempShapePos=vectorMatrixMult(curShapePos,shapeCurCTM);//将上述位置乘以当前形状的CTM以获得其当前位置
curShapePos.x=tempShapePos[0];
curShapePos.y=tempShapePos[1];
curShapePos.z=tempShapePos[2];
对于(int i=0;i<4;i++)
{
删除[]shapeCurCTM[i];
}
删除[]shapeCurCTM;
删除[]tempShapePos;
//////////////////计算当前交点的照明值////////////////////////////////////////////////////////////
GLdouble finalIntensity_R=0.0;
GLdouble finalIntensity_G=0.0;
GLdouble finalIntensity_B=0.0;//波长λ的最终强度
GLR=0.0;
GLdouble ambG=0.0;
GLB=0.0;
GLD双衍射=0.0;
GLdouble diffG=0.0;
GLdouble diffB=0.0;
vec3 N=vec3(hitA,curShapePos);
vec3*Nnorm=N.normalize();//单位长度曲面在交点处的法线
vec3**L=new vec3*[lightList.size()];//动态分配的vec3对象数组,每个对象随后将通过调用normalize在堆上分配(见下文)
//std::deque::迭代器m;
for(std::deque::iterator m=lightList.begin();m!=lightList.end();m++)//所有灯光m的值之和所有灯光m的集合,其中m={m0,m1,…,mn}
{
//每个灯光参数的复合rgb值…用于强度方程。。。
ambR+=m->amb[0];
ambG+=m->amb[1];
ambB+=m->amb[2];
衍射+=m->diff[0];
diff+=m->diff[1];
diffB+=m->diff[2];
L[std::distance(lightList.begin(),m)]=vec3(m->position,hitA).normalize();//由于调用normalize(),L中的每个隐式条目都包含一个在堆上分配的规范化向量
}
GLdouble globalAmbient[3]={ambR,ambG,ambB};//环境光的全局强度…也称为所有环境光RGB值的总和。。。
GLdouble objAmbient[4]={it->amb[0],it->amb[1],it->amb[2],it->amb[3]};//对象的环境色表示波长λ;在本例中,对象的R、G或B值表示环境色。。。
GLdouble mTotalIntensity[3]={(ambR+衍射),(ambG+扩散),(ambB+扩散)};//波长λ的光强度m;在本例中,光m的光颜色的R、G或B值
GLdouble globalDiffuse[3]={diffR,diffG,diffB};//全局漫反射系数
GLdouble objDiffuse[3]={it->diff[0],it->diff[1],it->diff[2]};//对象的漫反射颜色波长λ;在本例中,对象的漫反射颜色R、G或B值。
对于(int i=0;iamb[0])+((mTotalIntensity[0]*(globalDiffuse[0]*it->diff[0]*Nnorm->dot(*L[i]));
最终密度(globalAmbient[1]*it->amb[1])+((mTotalIntensity[1]*(globalDiffuse[1]*it->diff[1]*Nnorm->dot(*L[i]));
最终密度B+=((全局扩散[2]*it->amb[2])+((mTotalIntensity[2]*(全局扩散[2]*it->diff[2]*Nnorm->dot(*L[i]));
}
GLdouble LAMBDA[3]={finalIntensity_R,finalIntensity_G,finalIntensity_B};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//std::你能在Linux中尝试
valgrind
吗?我相信这是
delete[]L[I]
就像你用
shapecurtm所做的那样。也许还有别的事情!Nil说了什么——请看这里:关于如何分配和删除堆上的多维数组+我不确定
PlotPixel
做了什么,你是否通过调用
gldeletetemertures(1,&texId)删除了texId
使用完毕后?@cantcoose-OMG完成了!!!谢谢!我不得不删除纹理!但问题是:现在,我的GLUT显示窗口全是白色的…大概是因为