简体   繁体   English

阵列已删除或损坏

[英]array deleted or corrupted

I create an array with: 我创建一个数组:

MyClass *myInstance = new MyClass[1];

then I fill my class with data. 然后我用数据填满我的课。 After that, I expand the array like this: 之后,我像这样扩展数组:

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

and I fill the data from index 1 to myCount. 然后将数据从索引1填充到myCount。

After that I call a function with a callback. 之后,我使用回调函数调用函数。 Before the call all the data is correct, and at the start of the callback function, the data is lost and I get a EXC_BAD_ADDRESS . 在调用之前,所有数据都是正确的,并且在回调函数开始时,数据丢失了,我得到了EXC_BAD_ADDRESS The variable is a private variable, hence it can't be accessed from outside of the class. 该变量是一个私有变量,因此无法从类外部进行访问。 Which means that it can't be changed from the functions that will call the callback function. 这意味着不能从将调用回调函数的函数中更改它。

The filling only consists in setting variables of MyClass. 填充仅包含MyClass的设置变量。 But the whole reference to the class seems to be lost. 但是,整个类的参考似乎丢失了。

The project is a cross-platform iOS/Android project, which is being programmed with C++ and tested with XCode. 该项目是一个跨平台的iOS / Android项目,正在使用C ++进行编程并通过XCode进行测试。

Am I doing something wrong? 难道我做错了什么?

UPDATE: 更新:

I've redone it with std::vector, now it looks like this: 我已经用std :: vector重做了,现在看起来像这样:

in the .h I define: 在.h中,我定义:

std::vector<MyClass*> myInstances;

in the .m: I add 1 element 在.m中:我添加了1个元素

myInstances.push_back(new MyClass());

then I fill the variables within myClass. 然后我将变量填充到myClass中。 After that I expand myInstances like this: 之后,我像这样扩展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);
}

I'm still getting the same error, exactly at the same point :( 我仍然在同一点出现相同的错误:(

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.

You should consider looking into one of the STL containers such as std::vector or std::list . 您应该考虑研究其中一种STL容器,例如std :: vectorstd :: list If you need to use dynamic allocation, consider using a std::vector. 如果需要使用动态分配,请考虑使用std :: vector。 See std::unique_ptr . 参见std :: unique_ptr

Edit: 编辑:

You say in comments that you are calling 您在留言中说正在打电话

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

This calls setFacebookDelegate with the address of the specific instance you call it from. 这将使用您从中调用setFacebookDelegate的特定实例的地址进行调用。 You also say that it is operating as a singleton, but you are allocating multiple instances of it. 您还说它作为单例运行,但是您正在分配它的多个实例。

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.

A preliminary glance at the code suggests that it may be threaded - it has a manager system to handle multiple, potentially concurrent or overlapping requests, and I certainly wouldn't want my game stalling out every time facebook is slow to respond to a refresh request. 对代码的初步了解表明它可能是线程化的-它具有一个管理器系统来处理多个可能并发或重叠的请求,并且我当然不希望每次Facebook响应刷新请求的速度都很慢时,我的游戏都不会停止。

I'm still not clear why you need the vector at all -- you seem to have all the data stored somewhere else, and you only actually need the one vector entry, it's only function seems to be to get data from some other class and make it available to "MyClass", but it seems you should be able to do that thru a bridge or lookup class, eg a std::map<std::string /*FaceBookID*/, size_t localID> or std::map<std::string /*FaceBookID*/, RealClass*> . 我仍然不清楚为什么根本不需要向量-您似乎将所有数据存储在其他位置,并且实际上只需要一个向量条目,唯一的功能似乎是从其他类中获取数据,并且使它可用于“ MyClass”,但似乎您应该能够通过网桥或查找类来做到这一点,例如std::map<std::string /*FaceBookID*/, size_t localID>std::map<std::string /*FaceBookID*/, RealClass*>

You might try the following: 您可以尝试以下方法:

// 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;
}

If the plugin is NOT threaded, then the remaining possibility is that somewhere you are passing the address of the vector or the vector's data and you are clobbering it, or that the plugin has a memory leak which is causing it to stomp your vector. 如果插件没有线程化,那么剩下的可能性是您正在某个地方传递向量的地址或向量的数据,并且正在破坏它,或者该插件存在内存泄漏而导致其吞噬了向量。

To assist further, you'll need to include more of your code. 为了进一步提供帮助,您需要包含更多代码。

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

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