簡體   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