将 const char* 从 std::string 传递到 Lua 堆栈会变成 null
我有这段代码,我从游戏支持的不同类型的设备中收集设备 id,并将 lua 全局设置为具有当前设备的 id 值。 当我获取 iOS 设备的 id 时,我从混合的 C++/Objective-C 类接收到一个 const char* 并将其传递到 Lua 堆栈。一切都很好。 但是我从一段负责获取 Android 设备 ID 的代码中收到 std::string 。当我推送 deviceId.c_str() 时,我在 Lua 中得到 nil 。 我尝试从负责获取设备 ID 的代码中传递 const char* ,但是当指针从函数返回时,指针似乎出现了问题[这就是我决定返回字符串的原因,这样就可以正常工作了]。
我应该怎么做才能允许从 std::string 中传递 const char* 而不出现问题?
编辑: 我尝试过使用 strcpy 但它不起作用:/仍然有同样的问题。
所以.. 负责从不同设备收集 deviceId 的代码如下所示:
#include "DeviceInfo.h"
#include "DeviceInfoIOS.h"
#include "DeviceInfoAndroid.h"
#include <string>
USING_NS_CC;
extern "C" {
const char *getDeviceId() {
const char *deviceId;
CCLog("test");
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
DeviceInfoIOS ios;
deviceId = ios.getIOSDeviceId();
CCLog("iOS platform %s", deviceId);
#endif // CC_PLATFORM_IOS
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
CCLog("Android platform");
std::string tempId = getAndroidDeviceId();
CCLog("Android platform test %s", tempId.c_str());
char y[tempId.size() + 1];
strcpy(y, tempId.c_str());
deviceId = (const char*) y;
CCLog("Android platform %s", deviceId);
#endif // CC_PLATFORM_ANDROID
CCLog("Finished platform check");
return deviceId;
}
}
只是一个小注释:所有日志看起来都正常。 Devie id 顺利通过。
这就是我将设备 ID 传递给 Lua 的方法:
//deviceInfo
CCLog("DeviceInfo load");
const char *deviceId = getDeviceId();
CCLog("DeviceInfo %s", deviceId);
lua_pushstring(d_state, deviceId);
lua_setglobal(d_state, "DEVICE_ID");
同样在这里,日志文件包含设备 ID。
I've got this piece of code, where I gather device id from different types of devices supported by my game and set lua global to have value of id of current device.
When I get id of the iOS device I receive a const char* from a mixed C++/Objective-C class and pass it on to the Lua stack. It all works fine.
However I receive std::string from a piece of code responsible for getting Android device id. When I push deviceId.c_str() I get nil in Lua.
I've tried passing const char* from the code responsible for getting the device id, but then it seems something wrong goes on with the pointer when it's returned from function [that's why I decided to return string, it works fine this way].
What should I do to allow passing const char* out of std::string without problems?
EDIT:
I've tried using strcpy but it didn't work :/ still having the same problem.
So.. the code responsible for gathering deviceId from different devices looks like this:
#include "DeviceInfo.h"
#include "DeviceInfoIOS.h"
#include "DeviceInfoAndroid.h"
#include <string>
USING_NS_CC;
extern "C" {
const char *getDeviceId() {
const char *deviceId;
CCLog("test");
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
DeviceInfoIOS ios;
deviceId = ios.getIOSDeviceId();
CCLog("iOS platform %s", deviceId);
#endif // CC_PLATFORM_IOS
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
CCLog("Android platform");
std::string tempId = getAndroidDeviceId();
CCLog("Android platform test %s", tempId.c_str());
char y[tempId.size() + 1];
strcpy(y, tempId.c_str());
deviceId = (const char*) y;
CCLog("Android platform %s", deviceId);
#endif // CC_PLATFORM_ANDROID
CCLog("Finished platform check");
return deviceId;
}
}
Just a small note: All the logs look ok. Devie id is passed fine.
This is how I pass device id to Lua:
//deviceInfo
CCLog("DeviceInfo load");
const char *deviceId = getDeviceId();
CCLog("DeviceInfo %s", deviceId);
lua_pushstring(d_state, deviceId);
lua_setglobal(d_state, "DEVICE_ID");
Also in here, logfile contains the device id.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
getDeviceId
函数已损坏。tempId
和y
都是堆栈变量。一旦您返回,它们就会被摧毁。返回指向堆栈变量的指针始终是一个坏主意。您的函数应该返回一个
std::string
。如果失败,它应该返回一个char*
数组,它使用new
分配该数组,并且用户应该使用delete 取消分配该数组
。这通常就是为什么最好只返回std::string
的原因。或者,您可以将y
作为使用固定大小(而不是基于字符串的大小)的static
局部变量。Your
getDeviceId
function is broken. BothtempId
andy
are stack variables. They will be destroyed once you return. Returning pointers to stack variables is always a bad idea.Your function ought to return a
std::string
. Failing that, it should return achar*
array that it allocates withnew
, and that the user is expected to deallocate withdelete
. That's generally why it's preferable to just return astd::string
. Alternatively, you could delcarey
as astatic
local variable using a fixed size (rather than one based on the string).