Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

部分情况下为TArray创建拷贝至lua时会产生崩溃 #303

Merged
merged 1 commit into from
Dec 27, 2021
Merged

Conversation

C209
Copy link
Contributor

@C209 C209 commented Dec 10, 2021

拷贝TArray<FString>对象至lua即可复现。

@xuyanghuang-tencent
Copy link
Collaborator

用TArray来做复现这点不太理解,TypeInterface->Initialize 里做的工作只是用0填充,而后续会在TypeInterface->Copy里被覆盖掉。可以详细点说说?

@C209
Copy link
Contributor Author

C209 commented Dec 16, 2021

用TArray来做复现这点不太理解,TypeInterface->Initialize 里做的工作只是用0填充,而后续会在TypeInterface->Copy里被覆盖掉。可以详细点说说?

我这边是在TArrayLib里自己添加了一个Copy方法,直接利用了PushArray来实现,然后就在对元素类型为FString的TArray对象,在lua中调用Copy的时候发生了崩溃。

看调用栈发现崩在了TypeInterface->Copy里:由于FString不能简单的做Memcpy,所以会进入FString::operator=,这里在对FString::Data的拷贝过程就会出问题,因为Data没有被正确的初始化。

xuyanghuang-tencent added a commit that referenced this pull request Dec 24, 2021
@xuyanghuang-tencent
Copy link
Collaborator

方便贴一下你的Copy实现?或者直接在上面提交的测试用例里加。还是没明白为什么TypeInterface<FString>->Copy里会走到FString::operator=

用例里的流程是走到了void CopyInternal(T *Dest, const T *Src, FFalse NotTrivial) const { new(Dest) T(*Src); }然后通过拷贝构造函数来构造复制出来的FString元素。

PS:赞同流程上应该先初始化内存再使用,不过还是想捋捋清楚你说的场景。

@C209
Copy link
Contributor Author

C209 commented Dec 24, 2021

方便贴一下你的Copy实现?或者直接在上面提交的测试用例里加。还是没明白为什么TypeInterface<FString>->Copy里会走到FString::operator=

用例里的流程是走到了void CopyInternal(T *Dest, const T *Src, FFalse NotTrivial) const { new(Dest) T(*Src); }然后通过拷贝构造函数来构造复制出来的FString元素。

PS:赞同流程上应该先初始化内存再使用,不过还是想捋捋清楚你说的场景。

static int32 TArray_Copy(lua_State *L)
{
    int32 NumParams = lua_gettop(L);
    if (NumParams != 1)
    {
        UNLUA_LOGERROR(L, LogUnLua, Log, TEXT("%s: Invalid parameters!"), ANSI_TO_TCHAR(__FUNCTION__));
        return 0;
    }

    FLuaArray *SourceArray = (FLuaArray*)(GetCppInstanceFast(L, 1));
    if (!SourceArray)
    {
        UNLUA_LOGERROR(L, LogUnLua, Log, TEXT("%s: Invalid TArray!"), ANSI_TO_TCHAR(__FUNCTION__));
        return 0;
    }

    UnLua::PushArray(L, SourceArray->ScriptArray, SourceArray->Inner, true);
    return 1;
}

测试用例和我这里的区别在于:我是在Lua中创建的TArray对象,这里给过去的ITypeInterface是FStringPropertyDesc;而测试用例push的是TArray,给过去的则是TTypeInterface<FString>。FPropertyDesc::Copy的行为就是普通的拷贝而非拷贝构造了,看来这两个override的Copy行为可能不太一致。

@xuyanghuang-tencent xuyanghuang-tencent merged commit 19296fb into Tencent:master Dec 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants