gpt4 book ai didi

c++ - basic_string::_S_construct null 无效 - 不知道为什么?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:48:47 25 4
gpt4 key购买 nike

我正在转换一些代码以停止使用 char*,而是使用 std::string 来避免内存泄漏和/或缓冲区过载。

但是我遇到了一个出现上述错误的函数。我并没有真正改变太多 atm:

GuiText::GuiText(std::string t, int s, XeColor c) {
origText = NULL;
text = NULL;
size = s;
color = c;
alpha = c.a;
style = FTGX_JUSTIFY_CENTER | FTGX_ALIGN_MIDDLE;
maxWidth = 0;
wrap = false;
textDynNum = 0;
textScroll = SCROLL_NONE;
textScrollPos = 0;
textScrollInitialDelay = TEXT_SCROLL_INITIAL_DELAY;
textScrollDelay = TEXT_SCROLL_DELAY;

alignmentHor = ALIGN_CENTRE;
alignmentVert = ALIGN_MIDDLE;

if (!t.empty()) {
origText = strdup(t.c_str());
text = charToWideChar(gettext(t.c_str()));
}

for (int i = 0; i < 20; i++)
textDyn[i] = NULL;
}

所有代码都在这里https://github.com/siz-/xmplayer/blob/temp/source/libwiigui/gui_text.cpp#L32

因为我有很多 GuiText 实例,所以一切都很顺利,但是当我在第 59 行和 gui.h 中使用 const char* 注释函数以实际使我的代码使用正确的函数时,我得到了上述错误。我看不出为什么..

https://github.com/siz-/xmplayer/blob/temp/source/libwiigui/gui_text.cpp#L59 https://github.com/siz-/xmplayer/blob/temp/source/libwiigui/gui.h#L685

使用示例: https://github.com/siz-/xmplayer/blob/temp/source/menu.cpp#L427

有什么想法吗?我已经转换了所有的 gui_text.cpp,但还是有同样的错误,所以最好尝试查明问题并从那里开始。

希望你能帮助新手 ;-)

最佳答案

我假设以下两行试图初始化一个字符串:

origText = NULL;
text = NULL;

应该 1) 在初始化列表中初始化,并且 2) 如果它将为空,则不需要,并且 3) 无效(您不能将字符串初始化为 NULL) .

GuiText::GuiText(std::string t, int s, XeColor c) :
origText(t),
size(s),
color(c),
alpha(c.a),
style(FTGX_JUSTIFY_CENTER | FTGX_ALIGN_MIDDLE),
// etc
{
// etc
}

关于c++ - basic_string::_S_construct null 无效 - 不知道为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18769939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com