我正在使用Visual Studio 2010處理C++項目,一切正常,但是當我嘗試使用xcode 4運行我的程序時,它引發了Bas_Access異常。我認爲這是因爲內存泄漏,但我不確定如何解決問題。 我有以下功能:在xcode 4中分配操作員崩潰,在MSVS2010中運行正常
// Search is my class with x and y as members and here's is a constructor
// that I cretae in my Search.cpp class
Search& Search::operator=(const Search& search)
{
if(this != &search)
{
x = search.x;
y = search.y;
}
return *this;
}
,這裏是我如何調用函數:
Search searchStart(0,0);
//I created my tempSearch and initialized it with the start Search element
Search tempSearch(searchStart);
//bestSolution is a function that calculates the best neighbour node around the searchStart node, it returns a Search element. And stores it in a list in storage.
Search * tempCurrent=searchStart.bestSolution(&storage);
//Here I call my function
tempSearch=*tempCurrent;
我簡單地創建從現有元素的新的搜索元素,但它給了我的異常,在
x=search.x;
它與視覺工作室完美合作。
編輯: 我剛剛添加了我的函數被調用的代碼。請原諒我無法提供完整的代碼,因爲它很長。
編輯: 這是我的bestSolution功能:
Search * searchNode::Search::bestSolution(Storage *storage)
{
//listResult is a type defined as std::list<Search *> listResult.
listResult::iterator it, it1;
listResult tempList;
//I've initialized the result element at (0,0) because it was creating problems
// if uninitialized
Search *result=new Search(0,0);
//openList is a simple list of Search elements
if(!storage->openList.empty()){
for(it=storage->openList.begin();it!=storage->openList.end();it++)
{
tempList.push_back((*it));
}
tempList.reverse();
it1=tempList.begin();
// getDistanceCost is a function that calculates the heuristic distance
// between two points and works fine
int fCost=(*it1)->getDistanceCost();
for(it1=storage->openList.begin();it1!=storage->openList.end();it1++)
{
if((*it1)->getDistanceCost()<=fCost){
fCost=(*it1)->getDistanceCost();
result=(*it1);
}
}
}
return result;
}
它是編譯錯誤還是運行時錯誤? – 2012-03-14 09:53:26
'operator ='如何被調用?有沒有機會嘗試使用已經釋放的內存? – Asha 2012-03-14 09:54:25
它確實是'x = Search.x'還是它是'x = search.x'?這是一個巨大的差異。 – bitmask 2012-03-14 09:55:18