【问题标题】:Assignment operator crash in xcode 4, runs fine in MSVS2010xcode 4 中的赋值运算符崩溃,在 MSVS2010 中运行良好
【发布时间】: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


【解决方案1】:

我的猜测是bestSolution 正在返回一个指向在堆栈上分配的对象的指针。现在,当您尝试 tempSearch=*tempCurrent 时,您正在尝试将值复制到此无效指针中,这会导致未定义的行为。

编辑

查看bestSolution 方法的实现,我假设listResult 包含Search* 作为它的节点,就像您正在执行result=(*it1); 一样。看起来列表具有指针的Search 对象在插入列表后被删除。因此,您在列表中拥有的是一个无效指针。如果您尝试将任何内容复制到此无效指针所指向的内存中,您的程序将出现不可预知的行为。

【讨论】:

    【解决方案2】:

    根据您提供的信息,可以确定问题出在

    Search * tempCurrent=searchStart.bestSolution(&storage);
    

    它必须返回一些无效的指针(可能是NULL)。

    这个无效指针然后作为search 参数(更准确地说是&amp;search)传递给你的Search::operator=,然后它会尝试访问这个无效指针的x(以及后来的y)成员(search.xsearch.y)。

    【讨论】:

    • 好的,但是当我在 Visual Studio 中编译并且它对我有用时,没有问题,因为一开始我因为这些指针而被阻止,但现在它工作了,我认为它可以在任何地方工作。用xcode编译还是用VS编译有区别吗?
    • @Anila:很可能您依赖于某种未定义的行为。你能告诉我们bestSolution方法的代码(或至少内存分配部分)吗?
    • @Anila:可能是这样,特别是如果你依赖未定义的行为......我猜你会这样做。
    • @Asha 我在帖子中添加了我的 bestSolution 功能
    • @Anila:这就提出了一个问题,你在哪里以及如何填充你的storage-&gt;openList
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 2016-05-15
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多