简体   繁体   中英

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.

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 . 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. 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.

Am I doing something wrong?

UPDATE:

I've redone it with std::vector, now it looks like this:

in the .h I define:

std::vector<MyClass*> myInstances;

in the .m: I add 1 element

myInstances.push_back(new MyClass());

then I fill the variables within myClass. After that I expand myInstances like this:

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 . If you need to use dynamic allocation, consider using a std::vector. See 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. 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.

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*> .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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