字符串/ wchar内容不一致,取决于代码的位置?

时间:2019-01-04 17:39:22

标签: c++ visual-studio c++11

根据使用的免费功能,我有3种不同的结果:

struct __declspec(dllexport) TimerPair final
{
    long long Time{};
    string Descr;
};

template<typename... T>
wchar_t* Message(T &&... args)
{
    wchar_t message[100];
    swprintf(message, 100, forward<T>(args)...);
    return message;
}

template<typename... T>
void LogMessage(T &&... args)
{
    Logger::WriteMessage(Message(forward<T>(args)...));
}

const wchar_t* ToWchar(string arg)
{
    std::wstring widestr = std::wstring(arg.begin(), arg.end());
    return widestr.c_str();
}

以及单元测试中的代码:( oTimerPair结构)

// v1
LogMessage(L"%s : %.4fms\n", ToWchar(o.Descr), (float)o.Time / 1000000);

// v2
std::wstring widestr = std::wstring(o.Descr.begin(), o.Descr.end());
Logger::WriteMessage(Message(L"%s : %.4fms\n", widestr.c_str(), (float)o.Time / 1000000));

// v3
std::wstring widestr = std::wstring(o.Descr.begin(), o.Descr.end());
wchar_t message[100];
swprintf(message, 100, L"%s : %.4fms\n", widestr.c_str(), (float)o.Time / 1000000);
Logger::WriteMessage(message);

Logger::WriteMessage来自MSFT单元测试框架(using namespace Microsoft::VisualStudio::CppUnitTestFramework;

在前两种情况下,我得到如下结果:

enter image description here

而不是预期的(情况3):

enter image description here

似乎存在指针问题,但是代码看起来正确,尤其是使用value参数(long long)时。我想念什么?

更新。按照{strong> Omnifarious 的建议对局部变量使用static,在v1和v3中(当一个接一个地使用时)我得到不一致的结果:

enter image description here

1 个答案:

答案 0 :(得分:4)

解开所有这些都有些棘手,但是,这里肯定有一个问题。这段代码:

const wchar_t* ToWchar(string arg)
{
    std::wstring widestr = std::wstring(arg.begin(), arg.end());
    return widestr.c_str();
}

返回一个指向死内存的指针。当函数退出并且widestr超出范围时,它返回的指针将释放。如果您将其更改为此:

const wchar_t* ToWchar(string arg)
{
    static ::std::wstring widestr;

    widestr = std::wstring(arg.begin(), arg.end());
    return widestr.c_str();
}

它可能开始工作。但随后该函数将不再可重入,并且绝对肯定不再是线程安全的。

此功能的情况完全相同:

template<typename... T>
wchar_t* Message(T &&... args)
{
    wchar_t message[100];
    swprintf(message, 100, forward<T>(args)...);
    return message;
}

message超出范围,您将返回一个指向无效堆栈空间的指针。同样,如果您将功能更改为此:

template<typename... T>
wchar_t* Message(T &&... args)
{
    static wchar_t message[100];
    swprintf(message, 100, forward<T>(args)...);
    return message;
}

它可能会开始工作,但再次不再是可重入的或线程安全的。

我会重新考虑您的处理方式。这利用了C ++关于临时人员生命周期的保证来解决该问题:

#include <string>
#include <cstring>

extern void fake_logger_writemessage(wchar_t const *);

template <class T>
class WCharWrapper {
 public:
    WCharWrapper() = delete;  // Make it unconstructable
};

template <>
class WCharWrapper<wchar_t const *> {
 public:
    WCharWrapper() = delete;
    explicit WCharWrapper(wchar_t const *s) : s_(s) {}

    operator wchar_t const *() const { return s_; }

 private:
    wchar_t const * const s_;
};

template <>
class WCharWrapper<::std::wstring const &> {
 public:
    WCharWrapper() = delete;
    explicit WCharWrapper(::std::wstring const &s) : s_(s) {}

    operator wchar_t const *() const { return s_.c_str(); }

 private:
    ::std::wstring const &s_;
};

template <>
class WCharWrapper<char const *> {
 public:
    WCharWrapper() = delete;
    explicit WCharWrapper(char const *s) : s_(s, s + ::std::strlen(s)) {}

    operator wchar_t const *() const { return s_.c_str(); }

 private:
    ::std::wstring const s_;
};

template <>
class WCharWrapper<::std::string const &> {
 public:
    WCharWrapper() = delete;
    explicit WCharWrapper(::std::string const &s) : s_(s.begin(), s.end()) {}

    operator wchar_t const *() const { return s_.c_str(); }

 private:
    ::std::wstring const s_;
};

template <typename T>
T widen_strings(T &&arg)
{
    return ::std::forward(arg);
}

WCharWrapper<char const *> widen_strings(char const *arg)
{
    return WCharWrapper<char const *>(arg);
}

WCharWrapper<::std::string const &> widen_strings(::std::string const &arg)
{
    return WCharWrapper<::std::string const &>(arg);
}

// Capture non-const as well, to make sure they aren't passed through unchanged.
WCharWrapper<::std::string const &> widen_strings(::std::string &arg)
{
    return WCharWrapper<::std::string const &>(arg);
}

template <typename T>
T &&widen_strings(T && arg)
{
    return arg;
}

template<typename... T>
wchar_t const *Message(wchar_t *out, T &&... args)
{
    swprintf(out, widen_strings(::std::forward<T>(args))...);
    return out;
}

template<typename... T>
void LogMessage(T &&... args)
{
    wchar_t msgbuf[100];
    fake_logger_writemessage(Message(msgbuf, ::std::forward<T>(args)...));
}

This version I put on Godbolt应该可以正常工作。最后看一下如何使用它的示例。