fix memory leak and possible nullptr read

Signed-off-by: chcurran <82187351+carlitosan@users.noreply.github.com>
This commit is contained in:
chcurran
2021-08-17 16:40:48 -07:00
parent de9e991ccc
commit 1ad182105b
2 changed files with 19 additions and 18 deletions
@@ -88,9 +88,11 @@ namespace ScriptCanvasEditor
{
streamSource->Seek(0U, AZ::IO::GenericStream::ST_SEEK_BEGIN);
auto& scriptCanvasDataTarget = scriptCanvasAssetTarget->GetScriptCanvasData();
AZStd::vector<AZ::u8> byteBuffer(streamSource->GetLength());
AZStd::vector<AZ::u8> byteBuffer;
byteBuffer.resize_no_construct(streamSource->GetLength());
AZ::IO::ByteContainerStream<decltype(byteBuffer)> byteStreamSource(&byteBuffer);
const size_t bytesRead = streamSource->Read(byteBuffer.size(), byteBuffer.data());
scriptCanvasDataTarget.m_scriptCanvasEntity.reset(nullptr);
if (bytesRead == streamSource->GetLength())
{
@@ -98,31 +100,30 @@ namespace ScriptCanvasEditor
AZ::JsonDeserializerSettings settings;
// \todo more mapping stuff needs to go in the settings
settings.m_serializeContext = m_serializeContext;
byteStreamSource.Seek(0U, AZ::IO::GenericStream::ST_SEEK_BEGIN);
// attempt JSON deserialization...
if (JSRU::LoadObjectFromStreamByType
( &scriptCanvasDataTarget
, azrtti_typeid<ScriptCanvasData>()
, byteStreamSource
, &settings).IsSuccess())
( &scriptCanvasDataTarget
, azrtti_typeid<ScriptCanvasData>()
, byteStreamSource
, &settings).IsSuccess())
{
return AZ::Data::AssetHandler::LoadResult::LoadComplete;
}
#if defined(OBJECT_STREAM_EDITOR_ASSET_LOADING_SUPPORT_ENABLED)
else
{
// ...if there is a failure, check if it is saved in the old format
byteStreamSource.Seek(0U, AZ::IO::GenericStream::ST_SEEK_BEGIN);
// tolerate unknown classes in the editor. Let the asset processor warn about bad nodes...
if (AZ::Utils::LoadObjectFromStreamInPlace(byteStreamSource, scriptCanvasAssetTarget->GetScriptCanvasData()
#if defined(OBJECT_STREAM_EDITOR_ASSET_LOADING_SUPPORT_ENABLED)////
// ...if there is a failure, check if it is saved in the old format
byteStreamSource.Seek(0U, AZ::IO::GenericStream::ST_SEEK_BEGIN);
// tolerate unknown classes in the editor. Let the asset processor warn about bad nodes...
if (AZ::Utils::LoadObjectFromStreamInPlace
( byteStreamSource
, scriptCanvasDataTarget
, m_serializeContext
, AZ::ObjectStream::FilterDescriptor(assetLoadFilterCB, AZ::ObjectStream::FILTERFLAG_IGNORE_UNKNOWN_CLASSES)))
{
return AZ::Data::AssetHandler::LoadResult::LoadComplete;
}
{
return AZ::Data::AssetHandler::LoadResult::LoadComplete;
}
#endif//defined(OBJECT_STREAM_EDITOR_ASSET_LOADING_SUPPORT_ENABLED)
}
#endif
}
return AZ::Data::AssetHandler::LoadResult::Error;
@@ -734,7 +734,7 @@ namespace ScriptCanvasEditor
{
ui->statusTableView->clearSelection();
if (auto model = GetActiveData().second->GetModel())
if (auto model = GetActiveData().second ? GetActiveData().second->GetModel() : nullptr)
{
model->Clear();
model->RunValidation(m_activeGraphIds.scriptCanvasId);