指针问题(仅在发布版本中)
不确定如何描述这一点,但我在这里:
出于某种原因,当尝试创建我的游戏的发布版本进行测试时,它的敌人创建方面不起作用。
Enemies *e_level1[3];
e_level1[0] = &Enemies(sdlLib, 500, 2, 3, 128, -250, 32, 32, 0, 1);
e_level1[1] = &Enemies(sdlLib, 500, 2, 3, 128, -325, 32, 32, 3, 1);
e_level1[2] = &Enemies(sdlLib, 500, 2, 3, 128, -550, 32, 32, 1, 1);
这就是我创造敌人的方式。在调试配置中工作正常,但当我切换到发布配置时,它似乎无法正确初始化敌人。
对我来说,这似乎有点奇怪,它在调试中工作,但在发布中不起作用,任何帮助都可以帮助我了解主要是我做错了什么。
unsure how to go about describing this but here i go:
For some reason, when trying to create a release build version of my game to test, the enemy creation aspect of it isn't working.
Enemies *e_level1[3];
e_level1[0] = &Enemies(sdlLib, 500, 2, 3, 128, -250, 32, 32, 0, 1);
e_level1[1] = &Enemies(sdlLib, 500, 2, 3, 128, -325, 32, 32, 3, 1);
e_level1[2] = &Enemies(sdlLib, 500, 2, 3, 128, -550, 32, 32, 1, 1);
Thats how i'm creating my enemies. Works fine when in the debug configuration but when i switch to the release config, it doesn't seem to initialize the enemies correctly.
To me, this seems a bit strange that it works in debug but not in release, any help appreciated on what mostly is what i've done wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
并没有按照你的想法去做。如果是构造函数调用,它会创建一个临时变量,其地址存储在 e_level1[0] 中。当 e_level1[1] 初始化时,e_level1[0] 析构函数可能已经被调用。
你可能想做
doesn't do what you think it does. If it is the constructor call, it creates a temporary and its address is stored in e_level1[0]. When e_level1[1] is initialized e_level1[0] destructor is probably already called.
You probably want to do
其他人已经指出了错误,即临时对象立即被销毁,但到目前为止所有答案都使用手动内存管理 - 在 C++ 中使用例如
std::vector
对于这样的事情,例如然后您只需通过
enemies[2]
作为enemies[0]
访问Enemies
实例,它们就会被清理掉当矢量超出时自动 范围。Other people have already pointed out the error, namely that the temporaries are getting destroyed straight away, but all of the answers so far are using manual memory management - it's more idiomatic in C++ to use e.g.
std::vector
for something like this, e.g.Then you just access the
Enemies
instances asenemies[0]
throughenemies[2]
and they get cleaned up automatically when the vector goes out of scope.大多数调试模式都会为您将所有内存初始化为零。在正常执行中,它可能是随机的。
让你的编译器尽可能挑剔(你应该已经这样做了)并修复所有警告。
如果它仍然存在,您应该在 valgrind 等内存检查工具中运行它。
Most debug modes initialise all memory to zero for you. In normal execution it may be random.
Make your compiler as fussy as possible (you should have done this already) and fix all the warnings.
If it still persists, you should run it in a memory checking tool like valgrind.
该代码将指针初始化为指向立即销毁的临时对象。通过指针或引用访问不再存在的临时对象是未定义的行为。你想要:
The code initializes the pointers to point to temporary objects that are immediately destroyed. Accessing temporaries that no longer exist through pointers or references is undefined behavior. You want:
您似乎正在堆栈上创建 Enemies 对象,这意味着当它们超出范围时,您的 e_level1 指针将指向已被销毁的对象。 (证明:在 Enenmies 类析构函数中放置一个断点,然后查看断点何时被击中)因此,当您尝试取消引用它们时,您将得到垃圾。
您想要的是:
这将在堆上创建对象。这也意味着您需要使用以下方法销毁对象:
You seem to be creating the Enemies object on the stack, which means that the moment they go out of scope, your e_level1 pointers are pointing to objects that have been destroyed. (proof: put a breakpoint in the Enenmies class destructor and seee when that gets hit) Therefore, when you try to de-reference them, you'll get garbage.
What you want is:
That will create the object on the heap. It also means you will need to destroy the objects using: