glCallList 内存泄漏

Posted

技术标签:

【中文标题】glCallList 内存泄漏【英文标题】:glCallList Memory Leak 【发布时间】:2018-05-23 14:30:12 【问题描述】:

我正在通过创建带有向量、面和法线向量的glCallList 来创建 3D 对象。即使我在使用后删除了所有向量项目,我注意到内存正在填满,直到系统冻结。 我已经用 valgrind 分析了应用程序,但没有发现任何问题。 例如,这里我创建了一个 3D 对象:

void MyClass::drawObject() 


    glPushMatrix();

    std::vector<ObjCoordinate *> vertex;
    std::vector<ObjFace *> faces;
    std::vector<ObjCoordinate *> normals;

    vertex.push_back(new ObjCoordinate(-1.30287, -1.44034, 0.216688));
    vertex.push_back(new ObjCoordinate(-1.27961, -1.50539, 0.072052));
    vertex.push_back(new ObjCoordinate(-1.08061, -1.28665, 0.206139));
    vertex.push_back(new ObjCoordinate(-1.21638, -1.35257, 0.180025));
    vertex.push_back(new ObjCoordinate(-1.15207, -1.32099, 0.229603));
    vertex.push_back(new ObjCoordinate(-1.28515, -1.44447, 0.363248));
    vertex.push_back(new ObjCoordinate(-1.28928, -1.50988, 0.32576));
    vertex.push_back(new ObjCoordinate(-1.26158, -1.53761, 0.241082));
    vertex.push_back(new ObjCoordinate(-1.20301, -1.55354, 0.160082));
    vertex.push_back(new ObjCoordinate(-1.23054, -1.41658, 0.439494));
    vertex.push_back(new ObjCoordinate(-1.2319, -1.48643, 0.476053));
    vertex.push_back(new ObjCoordinate(-1.20001, -1.54897, 0.426928));
    vertex.push_back(new ObjCoordinate(-1.13637, -1.39263, 0.466446));
    vertex.push_back(new ObjCoordinate(-1.12158, -1.45057, 0.544793));
    vertex.push_back(new ObjCoordinate(-1.11357, -1.5188, 0.532664));
    vertex.push_back(new ObjCoordinate(-1.10002, -1.55842, 0.464288));
    vertex.push_back(new ObjCoordinate(-1.04322, -1.4075, 0.50493));
    vertex.push_back(new ObjCoordinate(-0.987058, -1.49763, 0.54012));
    vertex.push_back(new ObjCoordinate(-0.950752, -1.55954, 0.42481));
    vertex.push_back(new ObjCoordinate(-1.07206, -1.56899, 0.385537));
    vertex.push_back(new ObjCoordinate(-0.932486, -1.41533, 0.459069));
    vertex.push_back(new ObjCoordinate(-0.874065, -1.47041, 0.441795));
    vertex.push_back(new ObjCoordinate(-0.881756, -1.52362, 0.426011));
    vertex.push_back(new ObjCoordinate(-0.921452, -1.29853, 0.206971));
    vertex.push_back(new ObjCoordinate(-0.771931, -1.42509, 0.210098));
    vertex.push_back(new ObjCoordinate(-0.746773, -1.48725, 0.182829));
    vertex.push_back(new ObjCoordinate(-0.774339, -1.53451, 0.177566));
    vertex.push_back(new ObjCoordinate(-0.855455, -1.3279, 0.167282));
    vertex.push_back(new ObjCoordinate(-1.01916, -1.26652, 0.155538));
    vertex.push_back(new ObjCoordinate(-0.991061, -1.28652, 0.22452));
    vertex.push_back(new ObjCoordinate(-0.756721, -1.45955, 0.030918));
    vertex.push_back(new ObjCoordinate(-0.770647, -1.51682, 0.03924));
    vertex.push_back(new ObjCoordinate(-0.817663, -1.55395, 0.119195));
    vertex.push_back(new ObjCoordinate(-0.886225, -1.56266, 0.225088));
    vertex.push_back(new ObjCoordinate(-0.7967, -1.38249, 0.035854));
    vertex.push_back(new ObjCoordinate(-0.904208, -1.29591, 0.044522));
    vertex.push_back(new ObjCoordinate(-0.819127, -1.47748, -0.078676));
    vertex.push_back(new ObjCoordinate(-0.838126, -1.5358, -0.033324));
    vertex.push_back(new ObjCoordinate(-0.848241, -1.41404, -0.094209));
    vertex.push_back(new ObjCoordinate(-1.02568, -1.55382, 0.176562));
    vertex.push_back(new ObjCoordinate(-0.971872, -1.26754, 0.086063));
    vertex.push_back(new ObjCoordinate(-0.92238, -1.54838, -0.059034));
    vertex.push_back(new ObjCoordinate(-0.948484, -1.32027, -0.059503));
    vertex.push_back(new ObjCoordinate(-0.981524, -1.42006, -0.157504));
    vertex.push_back(new ObjCoordinate(-0.920155, -1.49055, -0.145955));
    vertex.push_back(new ObjCoordinate(-1.04709, -1.55412, -0.033783));
    vertex.push_back(new ObjCoordinate(-1.03018, -1.28025, 0.012423));
    vertex.push_back(new ObjCoordinate(-1.07872, -1.3425, -0.081606));
    vertex.push_back(new ObjCoordinate(-1.07475, -1.51965, -0.127059));
    vertex.push_back(new ObjCoordinate(-1.17664, -1.36255, -0.030069));
    vertex.push_back(new ObjCoordinate(-1.12364, -1.42699, -0.138141));
    vertex.push_back(new ObjCoordinate(-1.07618, -1.27108, 0.0936));
    vertex.push_back(new ObjCoordinate(-1.19638, -1.53617, -0.015344));
    vertex.push_back(new ObjCoordinate(-1.14774, -1.3108, 0.054986));
    vertex.push_back(new ObjCoordinate(-1.23368, -1.46416, -0.053383));
    vertex.push_back(new ObjCoordinate(-1.28552, -1.43121, 0.076094));
    normals.push_back(new ObjCoordinate(0.005, -0.9974, -0.0713));
    normals.push_back(new ObjCoordinate(-0.6866, 0.7197, 0.1033));
    normals.push_back(new ObjCoordinate(-0.2374, -0.9712, 0.0193));
    normals.push_back(new ObjCoordinate(-0.5388, 0.8243, 0.174));
    normals.push_back(new ObjCoordinate(-0.3386, 0.8976, 0.2822));
    normals.push_back(new ObjCoordinate(-0.505, -0.8554, 0.115));
    normals.push_back(new ObjCoordinate(-0.8265, 0.2736, 0.4919));
    normals.push_back(new ObjCoordinate(-0.6855, -0.6326, 0.3604));
    normals.push_back(new ObjCoordinate(-0.1487, -0.9887, 0.0186));
    normals.push_back(new ObjCoordinate(-0.3148, 0.9007, 0.2993));
    normals.push_back(new ObjCoordinate(-0.4678, -0.2077, 0.8591));
    normals.push_back(new ObjCoordinate(-0.3541, 0.7187, 0.5984));
    normals.push_back(new ObjCoordinate(-0.2487, -0.8586, 0.4483));
    normals.push_back(new ObjCoordinate(-0.0634, 0.9513, 0.3016));
    normals.push_back(new ObjCoordinate(-0.0757, 0.9291, 0.362));
    normals.push_back(new ObjCoordinate(0.1903, 0.4577, 0.8685));
    normals.push_back(new ObjCoordinate(-0.0352, -0.9962, -0.0801));
    normals.push_back(new ObjCoordinate(0.2365, 0.8754, 0.4217));
    normals.push_back(new ObjCoordinate(0.561, 0.3614, 0.7447));
    normals.push_back(new ObjCoordinate(0.6664, -0.299, 0.683));
    normals.push_back(new ObjCoordinate(0.2555, 0.8729, 0.4156));
    normals.push_back(new ObjCoordinate(0.0658, -0.9972, 0.0368));
    normals.push_back(new ObjCoordinate(0.8883, 0.1729, 0.4254));
    normals.push_back(new ObjCoordinate(0.8611, -0.255, 0.4399));
    normals.push_back(new ObjCoordinate(0.4462, -0.8647, 0.2308));
    normals.push_back(new ObjCoordinate(0.5542, 0.7458, 0.3698));
    normals.push_back(new ObjCoordinate(0.2842, -0.9528, 0.1063));
    normals.push_back(new ObjCoordinate(0.7447, 0.665, 0.0567));
    normals.push_back(new ObjCoordinate(0.4361, 0.8979, 0.0608));
    normals.push_back(new ObjCoordinate(0.4257, -0.8901, -0.163));
    normals.push_back(new ObjCoordinate(0.1881, -0.972, -0.1409));
    normals.push_back(new ObjCoordinate(0.7938, 0.4388, -0.421));
    normals.push_back(new ObjCoordinate(0.8547, -0.2721, -0.4422));
    normals.push_back(new ObjCoordinate(0.5532, 0.7275, -0.4058));
    normals.push_back(new ObjCoordinate(0.5494, 0.0479, -0.8342));
    normals.push_back(new ObjCoordinate(0.1958, 0.9286, -0.3154));
    normals.push_back(new ObjCoordinate(0.3177, 0.6087, -0.727));
    normals.push_back(new ObjCoordinate(-0.0255, -0.9401, -0.3399));
    normals.push_back(new ObjCoordinate(-0.0367, 0.9985, -0.0397));
    normals.push_back(new ObjCoordinate(-0.0541, 0.8452, -0.5316));
    normals.push_back(new ObjCoordinate(0.0016, 0.7004, -0.7138));
    normals.push_back(new ObjCoordinate(-0.4998, -0.5926, -0.6317));
    normals.push_back(new ObjCoordinate(-0.3383, 0.8943, -0.2928));
    normals.push_back(new ObjCoordinate(-0.6296, 0.4915, -0.6018));
    normals.push_back(new ObjCoordinate(-0.6613, 0.7193, -0.213));
    normals.push_back(new ObjCoordinate(-0.4933, 0.8696, 0.0197));
    normals.push_back(new ObjCoordinate(-0.4528, -0.8835, -0.1201));
    normals.push_back(new ObjCoordinate(-0.7207, 0.6918, -0.044));
    normals.push_back(new ObjCoordinate(-0.4009, 0.9095, 0.1101));
    normals.push_back(new ObjCoordinate(-0.9893, -0.0719, -0.1268));
    normals.push_back(new ObjCoordinate(-0.7662, -0.6414, -0.0406));
    normals.push_back(new ObjCoordinate(0.1119, -0.85, 0.5148));
    normals.push_back(new ObjCoordinate(-0.6144, 0.7732, 0.1572));
    normals.push_back(new ObjCoordinate(-0.9928, -0.0059, 0.1199));
    normals.push_back(new ObjCoordinate(-0.9144, -0.1563, 0.3735));
    normals.push_back(new ObjCoordinate(-0.1262, -0.9881, 0.0878));
    normals.push_back(new ObjCoordinate(-0.575, 0.3882, 0.7202));
    normals.push_back(new ObjCoordinate(-0.4591, -0.6816, 0.5698));
    normals.push_back(new ObjCoordinate(-0.0159, 0.9191, 0.3936));
    normals.push_back(new ObjCoordinate(-0.1244, 0.7864, 0.6051));
    normals.push_back(new ObjCoordinate(0.0306, -0.9892, 0.1436));
    normals.push_back(new ObjCoordinate(-0.0282, -0.1782, 0.9836));
    normals.push_back(new ObjCoordinate(0.8872, 0.4611, -0.0134));
    normals.push_back(new ObjCoordinate(0.3572, 0.5244, 0.773));
    normals.push_back(new ObjCoordinate(0.385, -0.7571, 0.5277));
    normals.push_back(new ObjCoordinate(0.6855, 0.5957, 0.4187));
    normals.push_back(new ObjCoordinate(0.787, -0.4994, 0.3622));
    normals.push_back(new ObjCoordinate(0.6173, 0.6939, 0.3709));
    normals.push_back(new ObjCoordinate(0.2872, -0.9518, 0.1076));
    normals.push_back(new ObjCoordinate(0.5118, -0.8538, -0.0955));
    normals.push_back(new ObjCoordinate(0.9622, -0.2497, -0.1086));
    normals.push_back(new ObjCoordinate(0.0349, -0.9979, -0.0546));
    normals.push_back(new ObjCoordinate(0.6244, 0.7798, -0.0448));
    normals.push_back(new ObjCoordinate(0.2659, 0.9494, 0.167));
    normals.push_back(new ObjCoordinate(0.8224, 0.2526, -0.5097));
    normals.push_back(new ObjCoordinate(-0.124, -0.1821, -0.9754));
    normals.push_back(new ObjCoordinate(0.6663, -0.5808, -0.4677));
    normals.push_back(new ObjCoordinate(0.4165, 0.9072, 0.0589));
    normals.push_back(new ObjCoordinate(-0.0661, -0.9978, 0.0082));
    normals.push_back(new ObjCoordinate(0.4853, -0.6298, -0.6065));
    normals.push_back(new ObjCoordinate(0.5503, 0.7349, -0.3963));
    normals.push_back(new ObjCoordinate(0.2805, -0.8024, -0.5267));
    normals.push_back(new ObjCoordinate(0.4113, 0.2129, -0.8863));
    normals.push_back(new ObjCoordinate(0.1927, 0.934, -0.3007));
    normals.push_back(new ObjCoordinate(-0.0434, 0.9897, -0.1365));
    normals.push_back(new ObjCoordinate(0.089, -0.8303, -0.5501));
    normals.push_back(new ObjCoordinate(-0.3589, 0.8528, -0.3793));
    normals.push_back(new ObjCoordinate(-0.1366, 0.6001, -0.7882));
    normals.push_back(new ObjCoordinate(-0.0018, -1, 0.0016));
    normals.push_back(new ObjCoordinate(-0.1506, -0.941, -0.3031));
    normals.push_back(new ObjCoordinate(-0.214, 0.9687, 0.1257));
    normals.push_back(new ObjCoordinate(-0.454, 0.6495, -0.6099));
    normals.push_back(new ObjCoordinate(-0.4923, -0.3548, -0.7948));
    normals.push_back(new ObjCoordinate(-0.1313, -0.986, -0.1026));
    normals.push_back(new ObjCoordinate(-0.68, -0.5854, -0.4414));
    normals.push_back(new ObjCoordinate(-0.7424, 0.5151, -0.4284));
    normals.push_back(new ObjCoordinate(-0.9317, -0.0547, -0.3591));
    normals.push_back(new ObjCoordinate(-0.6636, 0.7388, -0.1175));
    normals.push_back(new ObjCoordinate(-0.4752, 0.878, 0.0571));
    normals.push_back(new ObjCoordinate(-0.3939, -0.9131, -0.1053));
    normals.push_back(new ObjCoordinate(-0.9664, -0.2537, -0.0413));
    normals.push_back(new ObjCoordinate(0.1258, -0.857, 0.4997));
    normals.push_back(new ObjCoordinate(0.928, 0.3724, 0.0071));
    normals.push_back(new ObjCoordinate(0.865, -0.5001, -0.0409));
    normals.push_back(new ObjCoordinate(0.0412, -0.9976, -0.0554));
    normals.push_back(new ObjCoordinate(0.2071, 0.965, 0.161));
    normals.push_back(new ObjCoordinate(-0.0763, -0.2255, -0.9712));
    normals.push_back(new ObjCoordinate(-0.3754, 0.843, -0.3853));
    faces.push_back(new ObjFace(1, 40, 9, 20));
    faces.push_back(new ObjFace(2, 1, 4, 6));
    faces.push_back(new ObjFace(3, 8, 12, 9));
    faces.push_back(new ObjFace(4, 4, 5, 10));
    faces.push_back(new ObjFace(5, 5, 3, 10));
    faces.push_back(new ObjFace(6, 7, 12, 8));
    faces.push_back(new ObjFace(7, 6, 10, 11));
    faces.push_back(new ObjFace(8, 7, 11, 12));
    faces.push_back(new ObjFace(9, 12, 20, 9));
    faces.push_back(new ObjFace(10, 10, 3, 13));
    faces.push_back(new ObjFace(11, 11, 14, 15));
    faces.push_back(new ObjFace(12, 10, 13, 14));
    faces.push_back(new ObjFace(13, 12, 15, 16));
    faces.push_back(new ObjFace(14, 3, 29, 30));
    faces.push_back(new ObjFace(15, 3, 30, 13));
    faces.push_back(new ObjFace(16, 14, 17, 18));
    faces.push_back(new ObjFace(17, 40, 20, 34));
    faces.push_back(new ObjFace(18, 30, 21, 17));
    faces.push_back(new ObjFace(19, 21, 22, 18));
    faces.push_back(new ObjFace(20, 18, 22, 23));
    faces.push_back(new ObjFace(21, 30, 24, 21));
    faces.push_back(new ObjFace(22, 20, 19, 34));
    faces.push_back(new ObjFace(23, 22, 25, 26));
    faces.push_back(new ObjFace(24, 22, 26, 23));
    faces.push_back(new ObjFace(25, 23, 27, 19));
    faces.push_back(new ObjFace(26, 24, 28, 21));
    faces.push_back(new ObjFace(27, 27, 33, 19));
    faces.push_back(new ObjFace(28, 28, 35, 25));
    faces.push_back(new ObjFace(29, 24, 36, 28));
    faces.push_back(new ObjFace(30, 32, 38, 33));
    faces.push_back(new ObjFace(31, 38, 42, 33));
    faces.push_back(new ObjFace(32, 35, 39, 31));
    faces.push_back(new ObjFace(33, 31, 37, 32));
    faces.push_back(new ObjFace(34, 36, 43, 35));
    faces.push_back(new ObjFace(35, 39, 45, 37));
    faces.push_back(new ObjFace(36, 41, 47, 36));
    faces.push_back(new ObjFace(37, 43, 44, 39));
    faces.push_back(new ObjFace(38, 42, 49, 46));
    faces.push_back(new ObjFace(39, 29, 52, 41));
    faces.push_back(new ObjFace(40, 43, 47, 48));
    faces.push_back(new ObjFace(41, 43, 48, 44));
    faces.push_back(new ObjFace(42, 49, 55, 53));
    faces.push_back(new ObjFace(43, 47, 52, 54));
    faces.push_back(new ObjFace(44, 51, 50, 55));
    faces.push_back(new ObjFace(45, 50, 54, 56));
    faces.push_back(new ObjFace(46, 54, 52, 4));
    faces.push_back(new ObjFace(47, 53, 2, 8));
    faces.push_back(new ObjFace(48, 56, 4, 1));
    faces.push_back(new ObjFace(49, 52, 3, 5));
    faces.push_back(new ObjFace(50, 1, 2, 56));
    faces.push_back(new ObjFace(51, 8, 2, 7));
    faces.push_back(new ObjFace(52, 18, 16, 15));
    faces.push_back(new ObjFace(53, 6, 4, 10));
    faces.push_back(new ObjFace(54, 7, 1, 6));
    faces.push_back(new ObjFace(55, 7, 6, 11));
    faces.push_back(new ObjFace(56, 12, 16, 20));
    faces.push_back(new ObjFace(57, 11, 10, 14));
    faces.push_back(new ObjFace(58, 12, 11, 15));
    faces.push_back(new ObjFace(59, 13, 30, 17));
    faces.push_back(new ObjFace(60, 14, 13, 17));
    faces.push_back(new ObjFace(61, 20, 16, 19));
    faces.push_back(new ObjFace(62, 15, 14, 18));
    faces.push_back(new ObjFace(63, 31, 25, 35));
    faces.push_back(new ObjFace(64, 18, 17, 21));
    faces.push_back(new ObjFace(65, 19, 18, 23));
    faces.push_back(new ObjFace(66, 22, 21, 25));
    faces.push_back(new ObjFace(67, 23, 26, 27));
    faces.push_back(new ObjFace(68, 21, 28, 25));
    faces.push_back(new ObjFace(69, 19, 33, 34));
    faces.push_back(new ObjFace(70, 27, 32, 33));
    faces.push_back(new ObjFace(71, 32, 26, 31));
    faces.push_back(new ObjFace(72, 34, 42, 46));
    faces.push_back(new ObjFace(73, 28, 36, 35));
    faces.push_back(new ObjFace(74, 41, 30, 29));
    faces.push_back(new ObjFace(75, 31, 39, 37));
    faces.push_back(new ObjFace(76, 49, 44, 51));
    faces.push_back(new ObjFace(77, 32, 37, 38));
    faces.push_back(new ObjFace(78, 24, 41, 36));
    faces.push_back(new ObjFace(79, 34, 46, 40));
    faces.push_back(new ObjFace(80, 37, 45, 38));
    faces.push_back(new ObjFace(81, 35, 43, 39));
    faces.push_back(new ObjFace(82, 38, 45, 42));
    faces.push_back(new ObjFace(83, 39, 44, 45));
    faces.push_back(new ObjFace(84, 36, 47, 43));
    faces.push_back(new ObjFace(85, 41, 52, 47));
    faces.push_back(new ObjFace(86, 42, 45, 49));
    faces.push_back(new ObjFace(87, 54, 48, 47));
    faces.push_back(new ObjFace(88, 44, 48, 51));
    faces.push_back(new ObjFace(89, 40, 46, 9));
    faces.push_back(new ObjFace(90, 46, 49, 53));
    faces.push_back(new ObjFace(91, 29, 3, 52));
    faces.push_back(new ObjFace(92, 51, 48, 50));
    faces.push_back(new ObjFace(93, 49, 51, 55));
    faces.push_back(new ObjFace(94, 9, 46, 53));
    faces.push_back(new ObjFace(95, 53, 55, 2));
    faces.push_back(new ObjFace(96, 55, 50, 56));
    faces.push_back(new ObjFace(97, 2, 55, 56));
    faces.push_back(new ObjFace(98, 56, 54, 4));
    faces.push_back(new ObjFace(99, 4, 52, 5));
    faces.push_back(new ObjFace(100, 9, 53, 8));
    faces.push_back(new ObjFace(101, 1, 7, 2));
    faces.push_back(new ObjFace(102, 18, 19, 16));
    faces.push_back(new ObjFace(103, 31, 26, 25));
    faces.push_back(new ObjFace(104, 32, 27, 26));
    faces.push_back(new ObjFace(105, 34, 33, 42));
    faces.push_back(new ObjFace(106, 41, 24, 30));
    faces.push_back(new ObjFace(107, 49, 45, 44));
    faces.push_back(new ObjFace(108, 54, 50, 48));


    int listName = vertexObjectConverter.ConvertVerticesToObject(vertex, faces, normals);

    glColor3f(Red, Green, Blue);
    glTranslatef(0, 2, 0);
    glScalef(6.2, 6.2, 6.2);
    glCallList(listName);
    glPopMatrix();

    // Delete vector data to prevent memory leaks
    for (int i = 0; i < faces.size(); i++)
        delete faces[i];
    for (int i = 0; i < normals.size(); i++)
        delete normals[i];
    for (int i = 0; i < vertex.size(); i++)
        delete vertex[i];

为了从三角形中创建对象,我编写了这个函数:

int VertexObjectConverter::ConvertVerticesToObject(const std::vector<ObjCoordinate *> &vertex,
                                                   const std::vector<ObjFace *> &faces,
                                                   const std::vector<ObjCoordinate *> &normals) 
    int displayListName;
    displayListName = glGenLists(1);
    glNewList(displayListName, GL_COMPILE);
   //

   for (int i = 0; i < faces.size(); i++) 

        // Triangle faces
        glBegin(GL_TRIANGLES);
        glNormal3f(normals[faces[i]->facenum-1]->x,normals[faces[i]->facenum-1]->y,normals[faces[i]->facenum-1]->z);
        glVertex3f(vertex[faces[i]->faces[0]-1]->x,vertex[faces[i]->faces[0]-1]->y,vertex[faces[i]->faces[0]-1]->z);
        glVertex3f(vertex[faces[i]->faces[1]-1]->x,vertex[faces[i]->faces[1]-1]->y,vertex[faces[i]->faces[1]-1]->z);
        glVertex3f(vertex[faces[i]->faces[2]-1]->x,vertex[faces[i]->faces[2]-1]->y,vertex[faces[i]->faces[2]-1]->z);
        glEnd();

    
    glEndList();

    return displayListName;

我创建了两个结构体ObjCoordinateObjFace 来存储向量和人脸信息:

struct ObjCoordinate 
    float x, y, z;

    ObjCoordinate(float a, float b, float c) : x(a), y(b), z(c) ;
;

struct ObjFace 
    int facenum;
    int faces[4];
    bool four;

    ObjFace(int facen, int f1, int f2, int f3) : facenum(facen)  
        faces[0] = f1;
        faces[1] = f2;
        faces[2] = f3;
    

    ObjFace(int facen, int f1, int f2, int f3, int f4) : facenum(facen)  
        faces[0] = f1;
        faces[1] = f2;
        faces[2] = f3;
        faces[3] = f4;
        four = true;
    
;

场景只是通过多次调用draw函数来渲染:

/// Renders the scene
void RenderScene() 

    // Presets
    glLoadIdentity();
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
    glOrtho(-20, 20, -20, 20, -20, 20);

    // Scene Stuff
    setCameraPerspective(Perspective::ROTATE);

    // Orbit the camera around the origin
    if (rotateCamera) 

        cameraX = initCameraX * cos(theta) - initCameraY * sin(theta);
        cameraZ = initCameraX * sin(theta) + initCameraY * cos(theta);

        glTranslatef(cameraX, cameraY, cameraZ);
        gluLookAt(cameraX, cameraY, cameraZ, 0, 0, 0, 0, 1, 0);

        theta += 0.01;
        glPushMatrix();
    

    drawCoordinateAxes();
    createLight();

    // Draw element
    MyObject.drawObject();

    // Swap buffers
    glutSwapBuffers();

我有什么遗漏吗?是什么填满了记忆? 我的假设是 glCallList 与它有关,因为像球体和立方体这样的普通 openGL 对象是没有问题的,只有那些自定义创建的表单会触发这个问题。

【问题讨论】:

不应该在某个地方给glDeleteLists打电话吗? @BDL 每次绘制对象时都需要调用glDeleteLists吗? 一般来说:不会。但是当您不再需要它们时,您应该删除每个列表。由于每次绘制对象时都会创建一个新列表,我想您也应该每次都删除一个列表。 修复了,谢谢 【参考方案1】:

发生内存泄漏是因为每次调用 drawObject 时都会创建一个新的显示列表 (glNewList),但它们永远不会被删除。一个快速的解决方法是在绘制完显示列表后将其删除,但当前代码表明存在更根本的问题。

通常,只有当有一个 OpenGL 命令列表需要执行多次时,才应该使用显示列表。如果一组命令只执行一次(如在当前代码中),那么将它们首先存储在显示列表中是没有意义的。事实上,这会使代码比没有列表时更慢。

我建议在启动时编译一次列表(因为网格似乎无论如何都是静态的),然后在每一帧中使用这个列表来绘制对象。如果网格是完全动态的,我会完全删除列表(因为它只执行一次)。最后一个选项(完全动态的网格,多次绘制)是重用现有的显示列表,而不是创建一个新的。

请注意,显示列表在 2008 年左右在 OpenGL 3.0 中已被弃用。除非您必须支持旧项目,否则使用现代 OpenGL 应该是首选方式。

【讨论】:

以上是关于glCallList 内存泄漏的主要内容,如果未能解决你的问题,请参考以下文章

MFC内存泄漏调试

如何防止java中的内存泄漏

记录一次DialogFragment 内存泄漏

常见的内存泄漏原因及解决方法

Android ValueAnimator --内存泄漏

Android内存泄漏查找和解决