Merge pull request #2155 from aavenel/Fix2079

Fix #2079 Memory leak
pull/2158/head
Kim Kulling 2018-09-25 17:42:58 +02:00 committed by GitHub
commit 18879c0952
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 13 deletions

View File

@ -659,7 +659,7 @@ namespace glTF2
int width, height; int width, height;
private: private:
uint8_t* mData; std::unique_ptr<uint8_t[]> mData;
size_t mDataLength; size_t mDataLength;
public: public:
@ -674,7 +674,7 @@ namespace glTF2
{ return mDataLength; } { return mDataLength; }
inline const uint8_t* GetData() const inline const uint8_t* GetData() const
{ return mData; } { return mData.get(); }
inline uint8_t* StealData(); inline uint8_t* StealData();

View File

@ -688,7 +688,6 @@ T Accessor::Indexer::GetValue(int i)
inline Image::Image() inline Image::Image()
: width(0) : width(0)
, height(0) , height(0)
, mData(0)
, mDataLength(0) , mDataLength(0)
{ {
@ -704,7 +703,9 @@ inline void Image::Read(Value& obj, Asset& r)
if (ParseDataURI(uristr, uri->GetStringLength(), dataURI)) { if (ParseDataURI(uristr, uri->GetStringLength(), dataURI)) {
mimeType = dataURI.mediaType; mimeType = dataURI.mediaType;
if (dataURI.base64) { if (dataURI.base64) {
mDataLength = Util::DecodeBase64(dataURI.data, dataURI.dataLength, mData); uint8_t *ptr = nullptr;
mDataLength = Util::DecodeBase64(dataURI.data, dataURI.dataLength, ptr);
mData.reset(ptr);
} }
} }
else { else {
@ -717,8 +718,9 @@ inline void Image::Read(Value& obj, Asset& r)
this->mDataLength = this->bufferView->byteLength; this->mDataLength = this->bufferView->byteLength;
// maybe this memcpy could be avoided if aiTexture does not delete[] pcData at destruction. // maybe this memcpy could be avoided if aiTexture does not delete[] pcData at destruction.
this->mData = new uint8_t [this->mDataLength];
memcpy(this->mData, buffer->GetPointer() + this->bufferView->byteOffset, this->mDataLength); this->mData.reset(new uint8_t[this->mDataLength]);
memcpy(this->mData.get(), buffer->GetPointer() + this->bufferView->byteOffset, this->mDataLength);
if (Value* mtype = FindString(obj, "mimeType")) { if (Value* mtype = FindString(obj, "mimeType")) {
this->mimeType = mtype->GetString(); this->mimeType = mtype->GetString();
@ -729,10 +731,8 @@ inline void Image::Read(Value& obj, Asset& r)
inline uint8_t* Image::StealData() inline uint8_t* Image::StealData()
{ {
uint8_t* data = mData;
mDataLength = 0; mDataLength = 0;
mData = 0; return mData.release();
return data;
} }
inline void Image::SetData(uint8_t* data, size_t length, Asset& r) inline void Image::SetData(uint8_t* data, size_t length, Asset& r)
@ -747,7 +747,7 @@ inline void Image::SetData(uint8_t* data, size_t length, Asset& r)
bufferView->byteOffset = b->AppendData(data, length); bufferView->byteOffset = b->AppendData(data, length);
} }
else { // text file: will be stored as a data uri else { // text file: will be stored as a data uri
this->mData = data; this->mData.reset(data);
this->mDataLength = length; this->mDataLength = length;
} }
} }

View File

@ -818,7 +818,7 @@ void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset& r)
// Add the embedded textures // Add the embedded textures
for (size_t i = 0; i < r.images.Size(); ++i) { for (size_t i = 0; i < r.images.Size(); ++i) {
Image img = r.images[i]; Image &img = r.images[i];
if (!img.HasData()) continue; if (!img.HasData()) continue;
int idx = mScene->mNumTextures++; int idx = mScene->mNumTextures++;

View File

@ -101,6 +101,24 @@ TEST_F( utglTF2ImportExport, importBinaryglTF2FromFileTest ) {
EXPECT_TRUE( binaryImporterTest() ); EXPECT_TRUE( binaryImporterTest() );
} }
#ifndef ASSIMP_BUILD_NO_EXPORT
TEST_F(utglTF2ImportExport, importglTF2AndExportToOBJ) {
Assimp::Importer importer;
Assimp::Exporter exporter;
const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured.gltf", aiProcess_ValidateDataStructure);
EXPECT_NE(nullptr, scene);
EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "obj", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured_out.obj"));
}
TEST_F(utglTF2ImportExport, importglTF2EmbeddedAndExportToOBJ) {
Assimp::Importer importer;
Assimp::Exporter exporter;
const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF-Embedded/BoxTextured.gltf", aiProcess_ValidateDataStructure);
EXPECT_NE(nullptr, scene);
EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "obj", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF-Embedded/BoxTextured_out.obj"));
}
#endif // ASSIMP_BUILD_NO_EXPORT
TEST_F(utglTF2ImportExport, importglTF2PrimitiveModePointsWithoutIndices) { TEST_F(utglTF2ImportExport, importglTF2PrimitiveModePointsWithoutIndices) {
Assimp::Importer importer; Assimp::Importer importer;
//Points without indices //Points without indices