C ++ - std :: map.insert()分段错误

时间:2013-11-22 13:01:47

标签: c++ c++11 dictionary insert segmentation-fault

当我尝试插入地图时出现分段错误。

该功能如下所示:

void add(std::string id, std::string name)
{
Asset asset(nullptr, false, name);
mAssets.insert(std::make_pair<std::string, Asset>(id,asset)); <-- This line gives segfault
}

简单声明mAssets

std::map<assetID, Asset> mAssets;

Asset类是(草率)声明如下:

class Asset
{
public:
   Asset(T* a, bool l, std::string f) : asset(a), loaded(l), filename(f)
    {
    }
  Asset(const Asset& copy) 
{
  loaded   = copy.loaded;
  filename = copy.filename;
  asset    = new T();
  *asset   = *copy.asset;
}
  ~Asset()
{
  delete asset;
}
  Asset& operator=(const Asset& other)
{
  Asset temp(other);
  loaded = temp.loaded;
  filename = temp.filename;
  std::swap(asset,temp.asset);
  return *this;
}

  T*          asset;
  bool        loaded;
  std::string filename;
};

3 个答案:

答案 0 :(得分:5)

您的问题出现在您的复制构造函数中:

  asset    = new T();
  *asset   = *copy.asset;

我会留给你解决原因......

答案 1 :(得分:1)

在复制构造函数中,您将取消引用空指针:

*asset = *copy.asset

Asset asset(nullptr, false, name);

验证指针的颜色并避免取消引用空指针:

Asset(const Asset& copy) 
{
    loaded   = copy.loaded;
    filename = copy.filename;
    if (copy.asset)
    {
        asset    = new T();   // better may be asset = new T(copy)
        *asset   = *copy.asset;
    }
    else
    {
        asset = nullptr
    } 
}

答案 2 :(得分:0)

* asset = * copy.asset; //你应该检查资产是否为NULL然后检查资产

这是您的代码的工作方式:

#include <iostream>
#include <map>

using namespace std;

template<class T>
class Asset
{
  public:
    Asset(T* a, bool l, std::string f) : asset(a), loaded(l), filename(f)
  {
  }
    Asset(const Asset& copy) 
    {
      cout<<"copy"<<endl;
      loaded   = copy.loaded;
      filename = copy.filename;
      asset    = new T();
      if(&copy != NULL)
      {
      if(copy.asset != NULL)
        *asset   = *(copy.asset);
      }
    }
    ~Asset()
    {
      delete asset;
    }
    Asset& operator=(const Asset& other)
    {
      Asset temp(other);
      loaded = temp.loaded;
      filename = temp.filename;
      std::swap(asset,temp.asset);
      return *this;
    }

    T*          asset;
    bool        loaded;
    std::string filename;
};

std::map <string,Asset<int> > mAssets;

void add(std::string id, std::string name)
{
  Asset<int> asset(NULL, false, name);
  mAssets.insert(std::make_pair<std::string, Asset<int> >(id,asset)); //<-- This line gives segfault
}


int main()
{
  add("1","hi");
  cout<<"run"<<endl;

}