繁体   English   中英

阵列已删除或损坏

[英]array deleted or corrupted

我创建一个数组:

MyClass *myInstance = new MyClass[1];

然后我用数据填满我的课。 之后,我像这样扩展数组:

MyClass *tempList = new MyClass[myCount+1];
tempList[0] = myInstance[0];
myInstance=tempList;

然后将数据从索引1填充到myCount。

之后,我使用回调函数调用函数。 在调用之前,所有数据都是正确的,并且在回调函数开始时,数据丢失了,我得到了EXC_BAD_ADDRESS 该变量是一个私有变量,因此无法从类外部进行访问。 这意味着不能从将调用回调函数的函数中更改它。

填充仅包含MyClass的设置变量。 但是,整个类的参考似乎丢失了。

该项目是一个跨平台的iOS / Android项目,正在使用C ++进行编程并通过XCode进行测试。

难道我做错了什么?

更新:

我已经用std :: vector重做了,现在看起来像这样:

在.h中,我定义:

std::vector<MyClass*> myInstances;

在.m中:我添加了1个元素

myInstances.push_back(new MyClass());

然后我将变量填充到myClass中。 之后,我像这样扩展myInstances:

MyClass *tempInstance = myInstances[currentPlayerIndex];
myInstances.clear();
myInstances.push_back(tempInstance);
for (int i=1; i<friends->count()+1; i++)
{
    myInstances.push_back(new MyClass());
    //filling variables, like f.e. myInstances[i]->setScore(1000);
}

我仍然在同一点出现相同的错误:(

MyClass *myInstance = new MyClass[1];
// myInstance is a pointer variable which contains
// an address. It points to a memory location at which
// we have an array of ONE MyClass.

MyClass *tempList = new MyClass[myCount+1];
// creates a NEW allocation, without touching the original.
// tempList is now also a pointer variable. It contains
// an address, the address of a different memory location
// at which we now have 2 myClass instances. This is
// NOT the same location.

tempList[0] = myInstance[0];
// the MyClass instances at *myInstance and *tempList
// are now copies of each other, but only the first instances.

myInstance=tempList;
// myInstance is now pointing to tempList, but the array
// of MyClass instances that it pointed to are still
// allocated. Unfortunately, you no-longer have the
// address stored in any variables. This is called a leak.

您应该考虑研究其中一种STL容器,例如std :: vectorstd :: list 如果需要使用动态分配,请考虑使用std :: vector。 参见std :: unique_ptr

编辑:

您在留言中说正在打电话

EziSocialObject::sharedObject()->setFacebookDelegate(this);

这将使用您从中调用setFacebookDelegate的特定实例的地址进行调用。 您还说它作为单例运行,但是您正在分配它的多个实例。

MyClass* a = new MyClass;
MyClass* b = new MyClass; // No-longer a singleton.

EziSocialObject::sharedObject()->setFacebookDelegate(a);
// From this point on, a->fbUserPhotoCallback will be called.

对代码的初步了解表明它可能是线程化的-它具有一个管理器系统来处理多个可能并发或重叠的请求,并且我当然不希望每次Facebook响应刷新请求的速度都很慢时,我的游戏都不会停止。

我仍然不清楚为什么根本不需要向量-您似乎将所有数据存储在其他位置,并且实际上只需要一个向量条目,唯一的功能似乎是从其他类中获取数据,并且使它可用于“ MyClass”,但似乎您应该能够通过网桥或查找类来做到这一点,例如std::map<std::string /*FaceBookID*/, size_t localID>std::map<std::string /*FaceBookID*/, RealClass*>

您可以尝试以下方法:

// enable asserts -- only affects debug build.
#include <assert.h>

// Add a counter to track when we clear the vector,
// and track what the count was last time we called getFacebookPhoto
size_t g_vectorClears = 0;

static const size_t VECTOR_NOT_IN_USE = ~0;
size_t g_vectorUsed = VECTOR_NOT_IN_USE;

// When we clear the vector, check if we are using it.
    assert(g_vectorUsed == VECTOR_NOT_IN_USE);
    ++g_vectorClears;
    g_myInstances.clear();

// When you call getFacebookPhoto, store which clear we were on
    g_vectorUsed = g_vectorClears;
    blah->getFacebookPhoto(...);

// In MyClass->fbUserPhotoCallback, validate:
...
fbUserPhotoCallback(...)
{
    assert(g_vectorUsed == g_vectorClears);
    ...
    // at the end, set g_vectorUsed back to NOT_IN_USE.
    g_vectorUsed = VECTOR_NOT_IN_USE;
}

如果插件没有线程化,那么剩下的可能性是您正在某个地方传递向量的地址或向量的数据,并且正在破坏它,或者该插件存在内存泄漏而导致其吞噬了向量。

为了进一步提供帮助,您需要包含更多代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM