std::map 插入分段错误

发布于 2024-09-03 08:37:30 字数 1118 浏览 1 评论 0原文

为什么这段代码会因分段错误而停止:

class MapFile
{
 public:
   /* ... */
   std::map <unsigned int, unsigned int> inToOut;
};

bool MapFile::LoadMapFile( const wxString& fileName )
{
    /* ... */
   inToOut.insert( std::make_pair(input,output) );
}

但是当我输入“std::map inToOut;”时就在“inToOut.insert”之前它工作得很好:

class MapFile
{
 public:
   /* ... */
};

bool MapFile::LoadMapFile( const wxString& fileName )
{
    /* ... */
   std::map <unsigned int, unsigned int> inToOut;
   inToOut.insert( std::make_pair(input,output) );
}


好的。谢谢大家,在你们的帮助下,我似乎已经解决了这个问题。

问题出在我调用 LoadMapFile 的代码部分:

void WizardProductPage::OnTestButtonMapFile( wxCommandEvent& event )
{
   wxString filename;
   filename = locMapFile->GetValue();

   MapFile::LoadMapFile( filename );
}

应该是:

void WizardProductPage::OnTestButtonMapFile( wxCommandEvent& event )
{
   wxString filename;
   filename = locMapFile->GetValue();

   MapFile mapFile;
   mapFile.LoadMapFile( filename );
}

Why does this code stop with segmentation fault :

class MapFile
{
 public:
   /* ... */
   std::map <unsigned int, unsigned int> inToOut;
};

bool MapFile::LoadMapFile( const wxString& fileName )
{
    /* ... */
   inToOut.insert( std::make_pair(input,output) );
}

but when I put the "std::map inToOut;" just before "inToOut.insert" it works just fine :

class MapFile
{
 public:
   /* ... */
};

bool MapFile::LoadMapFile( const wxString& fileName )
{
    /* ... */
   std::map <unsigned int, unsigned int> inToOut;
   inToOut.insert( std::make_pair(input,output) );
}

?


OK. Thanks guys, it seems that I have fixed this issue thanks to your help.

The problem was in the part of the code where I've been calling the LoadMapFile :

void WizardProductPage::OnTestButtonMapFile( wxCommandEvent& event )
{
   wxString filename;
   filename = locMapFile->GetValue();

   MapFile::LoadMapFile( filename );
}

Should be :

void WizardProductPage::OnTestButtonMapFile( wxCommandEvent& event )
{
   wxString filename;
   filename = locMapFile->GetValue();

   MapFile mapFile;
   mapFile.LoadMapFile( filename );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一生独一 2024-09-10 08:37:30

我猜你的问题出在其他地方。以下代码可以正常工作:

class MapFile
{
public:
   std::map <unsigned int, unsigned int> inToOut;
   void LoadMapFile();
};

void MapFile::LoadMapFile()
{
   inToOut.insert( std::make_pair(1, 1) );
}

int main() {
   MapFile a;
   a.LoadMapFile();

   return 0;
}

尝试逐步调试或在此处发布整个代码,因为这没有问题。

另外,是的。如果您尝试在不锁定的情况下从不同线程同时执行该操作,则可能会导致段错误。

I guess your problem is somewhere else. The following code works ok:

class MapFile
{
public:
   std::map <unsigned int, unsigned int> inToOut;
   void LoadMapFile();
};

void MapFile::LoadMapFile()
{
   inToOut.insert( std::make_pair(1, 1) );
}

int main() {
   MapFile a;
   a.LoadMapFile();

   return 0;
}

Try step-by-step debugging or post your whole code here, because this has no problems in it.

Also, yes. If you're trying to do that operation from different threads simultaneosly without locking, it could cause segfault.

陈甜 2024-09-10 08:37:30

最有可能的是,您有缓冲区溢出或错误的指针,导致您在映射是您的类的成员时将其丢弃。当您拥有一个自动变量的类时,它位于内存中的其他位置,而您最初的错误正在浪费其他一些内存。

您应该在内存调试器下运行代码。如果您使用的是 Linux,我会推荐 Valgrind

Most likely you have a buffer overflow or a bad pointer that has caused you to trash the map when it's a member of your class. When you have the class an an auto variable, it is somewhere else in memory and your original bug is trashing some other piece of memory.

You should run your code under a memory debugger. If you are on Linux, I would recommend Valgrind

水晶透心 2024-09-10 08:37:30

也许您的应用程序是多线程的,并且您没有锁定对 map 的插入。第二个变体不与其他线程共享map

Maybe your application is multi-threaded and you are not locking insertion into map. Second variant doesn't share map with other threads.

淡水深流 2024-09-10 08:37:30

您可能调用了已删除对象的成员函数。

例如。

MapFile *p;
{
  MapFile a;
  p = &a;
}

p->LoadMapFile("test.map");

会产生你所描述的错误。第二种情况不会,因为您在任何时候都不会取消引用 this 指针。

编辑:也许不是,如果输入、输出变量是成员数据,那么我的答案将是不正确的。

It's possible that you called the member function on a object that has been deleted.

eg.

MapFile *p;
{
  MapFile a;
  p = &a;
}

p->LoadMapFile("test.map");

Would produce the error you described. The second case would not since you are not dereferencing the this pointer at any time.

edit: Maybe not, in the case that the input,output variables are member data then my answer would be incorrect.

⒈起吃苦の倖褔 2024-09-10 08:37:30

此类问题的其他原因可能是使用带有自定义比较器和/或分配器的映射,并且没有相应地初始化映射,这可能在任何随机点生成分段错误,即:第一个插入可以完美工作,但在第二个插入上,当调用比较函数时,可能会出现分段错误。

Other reason for this kind of problem could be using a map with custom comparator and/or allocator and not initializing the map accordingly, which could generate segmentation faults at any random point, ie: the first insert could work perfectly, but on the second one, when the compare function is called, there could be a segmentation fault.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文