将 const char* 从 std::string 传递到 Lua 堆栈会变成 null

发布于 2024-12-23 16:46:39 字数 1592 浏览 0 评论 0原文

我有这段代码,我从游戏支持的不同类型的设备中收集设备 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 技术交流群。

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

发布评论

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

评论(1

所有深爱都是秘密 2024-12-30 16:46:39

您的 getDeviceId 函数已损坏。 tempIdy 都是堆栈变量。一旦您返回,它们就会被摧毁。返回指向堆栈变量的指针始终是一个坏主意。

您的函数应该返回一个 std::string。如果失败,它应该返回一个 char* 数组,它使用 new 分配该数组,并且用户应该使用 delete 取消分配该数组。这通常就是为什么最好只返回 std::string 的原因。或者,您可以将 y 作为使用固定大小(而不是基于字符串的大小)的 static 局部变量。

Your getDeviceId function is broken. Both tempId and y 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 a char* array that it allocates with new, and that the user is expected to deallocate with delete. That's generally why it's preferable to just return a std::string. Alternatively, you could delcare y as a static local variable using a fixed size (rather than one based on the string).

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