内存分配崩溃 [英] Memory Allocation Crash

查看:262
本文介绍了内存分配崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了一个奇怪的问题,我不明白。我不是C / C ++的专家,所以忍受我。我有一个NPC类,它派生自Player类,它派生自Sprite类。 sprite类包含一个setupAnimation函数,它分配一个在纹理上包含坐标的浮点数组,数组的每个元素都指向一个动画帧。这很好,工作正常。



但是,当我添加一个指针数组到NPC类时会出现问题。这些指针是一个Item类类型。当我添加这个数组,它工作正常,如果数组很小(10是我测试的大小),但会在分配浮点数组时,如果前面提到的纹理坐标会崩溃,如果大小稍大(100是大小I



这里有一些代码片段显示了我上面提到的材料:



Item类数组指针:

  engItem * itsLoot [100]; //这里的100,崩溃发生如下所示

纹理坐标及其分配对应: / p>

  GLfloat * itsTextureXData; 
GLfloat * itsTextureYData;

...

animationFile>> frameCount; //测试,值是正确的

engDeallocate(getTextureXData(),true); // Works fine
itsTextureXData = new GLfloat [frameCount]; //这是发生崩溃的地方

engDeallocate(getTextureYData(),true);
itsTextureYData = new GLfloat [frameCount];

这段代码是从Sprite类派生的每个类的基础。我不能理解的是为什么额外的90指针导致浮点分配期间的问题。操作系统:Windows Vista 32位,编译器:Visual C ++ 9.0,程序运行时内存:〜17,600k,系统内存: 〜2GB



记住这一点,我不能看到它是内存运行干燥,我不能束缚指针数组如何导致分配失败。如上所述,分配在从Sprite(以及Sprite本身)派生的所有其他类中工作良好,但是一旦这个指针数组被添加了NPC类,NPC将不再分配这个纹理数据float数组而不崩溃。 / p>

解决方案

在你的程序的某个地方,你可能有一些内存错误(写过一个数组的结尾,存储器等)。当发生这种情况时,可能会覆盖内存分配系统使用的结构,导致它在下一次分配时崩溃。使用 Valgrind 等工具来查找错误位置可能有助于运行您的程序。 编辑:我刚刚意识到,您使用的是Windows和Valgrind只适用于Linux。如果你的代码是足够便携的,那么尝试Linux和Valgrind会相对容易。如果您不能这样做,请参阅有一个很好的Valgrind替代windows? on SO。 (不幸的是,它看起来像是大部分是商业的,而不是像Valgrind这样的免费开放源代码。)



避免这种情况最好的方法是使用C ++集合, code> std :: vector 或 std :: list 。如果使用这些集合,请确保将复制保持为最小(例如,如果不修改原始对象的副本,则使用 const 引用作为参数,而不是原始对象的副本,等等),或者你会得到非常糟糕的表现(看起来你正在做一个游戏,所以这是很重要的)。


I have stumbled accross a strange problem in which I cannot understand. I am not an expert at C/C++ so bear with me. I have an NPC class, which derives from a Player class, which derives from a Sprite class. The sprite class contains a setupAnimation function, which allocates an array of floats containing coordinates on the texture, each element of the array refers to a frame of animation. This is all good and works fine.

However, a problem occurs when I add an array of pointers to the NPC class. These pointers are of an Item class type. When I add this array, it works fine if the array is small (10 was the size I tested), but will crash when allocating the array of floats for texture coordinates mentioned earlier if the size is a bit larger (100 was the size I tested).

Here are some code fragments showing the material I mentioned above:

The Item class array of pointers:

    engItem* itsLoot[100]; // With 100 here, the crash occurs as shown below

The texture coordinates and their allocation counterparts:

    GLfloat* itsTextureXData;
    GLfloat* itsTextureYData;

    ...

    animationFile >> frameCount; // Tested, the value is correct

    engDeallocate(getTextureXData(), true); // Works fine
    itsTextureXData = new GLfloat[frameCount]; // This is where the crash occurs

    engDeallocate(getTextureYData(), true);
    itsTextureYData = new GLfloat[frameCount];

This fragment of code is the basis of every class that derives from the Sprite class. What I can't understand is why an extra 90 pointers causes a problem during the float allocation. Just a bit of software information to go with this

OS: Windows Vista 32-bit, Compiler: Visual C++ 9.0, Program runtime memory: ~17,600k, System memory: ~2GB

With this in mind, I can't see it being memory running dry, and I can't tie in how an array of pointers causes the allocation to fail. As mentioned, the allocation works fine in all other classes derived from Sprite (as well as Sprite itself), but once this array of pointers is added the the NPC class, the NPC will no longer allocate this texture data float array without crashing.

解决方案

Somewhere else in your program, you probably have some sort of memory error (wrote past the end of an array, wrote to freed memory, etc.). When this happens, it is possible that you can overwrite structures used by the memory allocation system, causing it to crash on the next allocation. It might help to run your program with a tool like Valgrind to find where the error is. EDIT: I just realized that you're using Windows and Valgrind only works on Linux. If your code is portable enough, it would be relatively easy to try Linux and Valgrind. If you can't do that, see Is there a good Valgrind substitute for windows? on SO. (Unfortunately, it looks like most of them are commercial, not free and open source like Valgrind.)

The best way to avoid this is to use C++ collections such as std::vector or std::list rather than arrays. If you use these collections, make sure you keep the copying to a minimum (e.g. use const references as parameters rather than copies of the original object if you don't modify it, etc.), or you will get very bad performance (and it looks like you're doing a game, so this is important).

这篇关于内存分配崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆