地图用字符串坏了? [英] map with string is broken?

查看:153
本文介绍了地图用字符串坏了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的。我看不到我在做错什么。

Yes. I can't see what I'm doing wrong.

地图是:string,int

The map is: string, int

这里是方法

bange::function::Add(lua_State *vm){  
 //userdata, function  
 if (!lua_isfunction(vm, 2)){  
  cout << "bange: AddFunction: First argument isn't a function." << endl;  
  return false;}  
 void *pfunction = const_cast<void *>(lua_topointer(vm, 2));  
 char key[32] = {0};  
 snprintf(key, 32, "%p", pfunction);  
 cout << "Key: " << key << endl;  
 string strkey = key;  
 if (this->functions.find(strkey) != this->functions.end()){  
     luaL_unref(vm, LUA_REGISTRYINDEX, this->functions[strkey]);}  
 this->functions[strkey] = luaL_ref(vm, LUA_REGISTRYINDEX);  
 return true;  




$ b

Ok, when the code is executed, It crashes and prints this output:

Program received signal SIGSEGV, Segmentation fault.  
0x00007ffff6e6caa9 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >
::compare(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const () from /usr/lib/libstdc++.so.6

严重的是,我的代码怎么了?感谢您的帮助。

Seriously, what's wrong with my code. Thanks for help.

编辑1:

解决方案仍然失败。我尝试直接插入一个字符串,但它给出了相同的错误。

Ok, I've done the solution and still fails. I've tried directly inserting a string but it gives the same error.

我们来看看,对象是一个 bange :: scene 继承自 bange :: function 。我用 lua_newuserdata 创建对象:

Let's see, the object is a bange::scene inherited from bange::function. I create the object with lua_newuserdata:

bange::scene *scene = static_cast<bange::scene *>(lua_newuserdata(vm, sizeof(bange::scene)));
(...)
scene = new (scene) bange::scene(width, height, nlayers, vm);

我需要这个用于Lua垃圾收集。现在从Lua访问 bange :: function :: Add

I need this for Lua garbage collection. Now the access to bange::function::Add from Lua:

static int bangefunction_Add(lua_State *vm){
    //userdata, function
    bange::function *function = reinterpret_cast<bange::function *>(lua_touserdata(vm, 1));
    cout &lt&lt "object with bange::function: " &lt&lt function << endl;
    bool added = function->bange::function::Add(vm);
    lua_pushboolean(vm, static_cast<int>(added));
    return 1;
}

Userdata bange :: scene 存储在Lua中。事实上,知道用户数据场景当我之前创建了场景时,对象的方向是一样的。我需要 reinterpret_cast ,然后调用该方法。

Userdata bange::scene is stored in Lua. Knowing that userdata scene is, in fact, the object's direction is the same when I've created the scene before. I need the reinterpret_cast, and then call the method. The pointer "this" is still the same direction inside the method.

解决

我在 bange :: function 构造函数中做了一个小小的测试,它没有问题。

I did a small test in the bange::function constructor which works without problems.

bange::function::function(){  
    string test("test");  
    this->functions["test"] = 2;  
}  

我终于注意到问题是

bange :: function * function = reinterpret_cast< bange :: function *>(lua_touserdata(vm,1));

因为对象是 bange :: scene 而不是 bange :: function 我承认它,一个指针损坏),这似乎更多的代码设计问题。所以这在某种程度上是解决的。感谢大家。

because the object is bange::scene and no bange::function (I admit it, a pointer corruption) and this seems more a code design issue. So this, in a way, is solved. Thanks everybody.

推荐答案

这段代码可能没有错。

当您的代码尝试读取或写入未映射到进程的内存时,会出现分段故障。它发生的一点可能与错误的位置无关。

A Segmentation fault happens when your code tries to read or write to memory that isn't mapped to your process. The point that it happens might not be related to where the bug is.

在程序的某个时刻,在分段错误之前,一些代码使堆成堆。

At some point in your program, before the segmentation fault, some code munged the heap. This could be


  1. 访问已删除/释放的指针

  2. 覆盖数组的边界

  3. 将对象投射到错误的类型并使用它

这些东西都不得立即崩溃 - - 他们可以默默地破坏记忆。在某些时候,当使用该内存时,它也可能不会崩溃(只是默默地损坏其他内存)。在某些时候你会崩溃,但是这一点可能与这个问题完全无关。

None of these things has to crash immediately -- they could just silently corrupt memory. At some point later, when that memory is used, it might also not crash (just silently corrupt other memory). At some point you crash, but that point might be completely unrelated to the problem.

调试策略是让第一个损坏崩溃。有一些方法可以做到这一点。

The strategy for debugging is to get the first corruption to crash. There are a few ways to do that.


  1. 使用调试堆。以下是GCC在Linux上的一些可能性 GCC的调试堆/ STL调试等效?

Valgrind: http:// valgrind .org /

Valgrind: http://valgrind.org/

这篇关于地图用字符串坏了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆