【发布时间】:2012-03-14 09:51:12
【问题描述】:
我正在使用 Visual Studio 2010 开发 C++ 项目,一切都很好,但是当我尝试使用 xcode 4 运行我的程序时,它引发了 Bas_Access 异常。我认为这是因为内存泄漏,但我不确定如何解决这个问题。 我有以下功能:
// 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;
它与视觉工作室完美配合。
编辑: 我刚刚添加了调用我的函数的代码。实在是太长了,无法提供完整的代码。
编辑: 这是我最好的解决方案功能:
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;
}
【问题讨论】:
-
是编译错误还是运行时错误?
-
operator=是如何被调用的?是否有任何机会尝试使用已释放的内存? -
真的是
x=Search.x还是x=search.x?这是一个巨大的差异。 -
错误是什么?另外,为什么不将其实现为复制构造函数?
-
@LuchianGrigore 这是一个运行时错误我想因为当我只是构建项目时没有问题。
标签: c++ visual-studio-2010 xcode4 exc-bad-access