Skip to content

Commit 1468dc7

Browse files
committed
Deallocate asset data
1 parent b6d1e96 commit 1468dc7

File tree

6 files changed

+44
-14
lines changed

6 files changed

+44
-14
lines changed

include/scratchcpp/asset.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class LIBSCRATCHCPP_EXPORT Asset : public Entity
1919
Asset(const std::string &name, const std::string &id, const std::string &format);
2020
Asset(const Asset &) = delete;
2121

22-
virtual ~Asset() { }
22+
virtual ~Asset();
2323

2424
void setId(const std::string &id);
2525

src/project_p.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ bool ProjectPrivate::tryLoad(IProjectReader *reader)
121121
// Load asset data
122122
for (size_t i = 0; i < assets.size(); i++) {
123123
const std::string &data = assetData[i];
124-
assets[assetNames[i]]->setData(data.size(), static_cast<void *>(const_cast<char *>(data.c_str())));
124+
char *ptr = (char *)malloc(data.size() * sizeof(char));
125+
strncpy(ptr, data.data(), data.size());
126+
assets[assetNames[i]]->setData(data.size(), ptr);
125127
}
126128

127129
} else {

src/scratch/asset.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ Asset::Asset(const std::string &name, const std::string &id, const std::string &
1414
impl->updateFileName(id);
1515
}
1616

17+
/*! Destroys Asset. */
18+
Asset::~Asset()
19+
{
20+
if (impl->data) {
21+
free(impl->data);
22+
impl->data = nullptr;
23+
}
24+
}
25+
1726
/*! Sets the ID (MD5 hash) of the asset file. */
1827
void Asset::setId(const std::string &id)
1928
{
@@ -51,9 +60,12 @@ unsigned int Asset::dataSize() const
5160
return impl->dataSize;
5261
}
5362

54-
/*! Sets the asset data. */
63+
/*! Sets the asset data (will be deallocated when the object is destroyed). */
5564
void Asset::setData(unsigned int size, void *data)
5665
{
66+
if (impl->data)
67+
free(impl->data);
68+
5769
impl->dataSize = size;
5870
impl->data = data;
5971
processData(size, data);

src/scratch/asset_p.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct AssetPrivate
1919
std::string name;
2020
std::string dataFormat;
2121
std::string fileName;
22-
const void *data = nullptr;
22+
void *data = nullptr;
2323
unsigned int dataSize = 0;
2424
Target *target = nullptr;
2525
};

test/assets/asset_test.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,24 @@ TEST(AssetTest, Data)
2929
TestAsset asset;
3030
ASSERT_EQ(asset.data(), nullptr);
3131

32-
static char data[5] = "abcd";
32+
char *data = (char *)malloc(4 * sizeof(char));
33+
strncpy(data, "abcd", 4);
34+
3335
asset.setData(5, data);
3436
ASSERT_EQ(asset.data(), data);
3537
ASSERT_EQ(asset.size, 5);
3638
ASSERT_EQ(asset.processedData, data);
3739
ASSERT_EQ(asset.callCount, 1);
40+
41+
// Should deallocate in setData()
42+
data = (char *)malloc(11 * sizeof(char));
43+
strncpy(data, "Hello world!", 11);
44+
45+
asset.setData(5, data);
46+
ASSERT_EQ(asset.data(), data);
47+
ASSERT_EQ(asset.size, 5);
48+
ASSERT_EQ(asset.processedData, data);
49+
ASSERT_EQ(asset.callCount, 2);
3850
}
3951

4052
TEST(AssetTest, Target)

test/assets/sound_test.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,20 @@ TEST_F(SoundTest, ProcessData)
5959
Sound sound("sound1", "a", "wav");
6060
sound.setRate(44100);
6161

62-
const char *data = "abc";
63-
void *dataPtr = const_cast<void *>(static_cast<const void *>(data));
62+
char *data = (char *)malloc(4 * sizeof(char));
63+
strncpy(data, "abcd", 4);
6464

6565
EXPECT_CALL(*m_player, isLoaded()).WillOnce(Return(false));
66-
EXPECT_CALL(*m_player, load(3, dataPtr, 44100)).WillOnce(Return(true));
67-
sound.setData(3, dataPtr);
66+
EXPECT_CALL(*m_player, load(3, data, 44100)).WillOnce(Return(true));
67+
sound.setData(3, data);
68+
69+
// Should deallocate in setData()
70+
data = (char *)malloc(11 * sizeof(char));
71+
strncpy(data, "Hello world!", 11);
6872

6973
EXPECT_CALL(*m_player, isLoaded()).WillOnce(Return(true));
7074
EXPECT_CALL(*m_player, load).Times(0);
71-
sound.setData(3, dataPtr);
75+
sound.setData(3, data);
7276
}
7377

7478
TEST_F(SoundTest, SetVolume)
@@ -151,12 +155,12 @@ TEST_F(SoundTest, Clone)
151155
sound->setRate(44100);
152156
sound->setSampleCount(10000);
153157

154-
const char *data = "abc";
155-
void *dataPtr = const_cast<void *>(static_cast<const void *>(data));
158+
char *data = (char *)malloc(4 * sizeof(char));
159+
strncpy(data, "abcd", 4);
156160

157161
EXPECT_CALL(*m_player, isLoaded()).WillOnce(Return(false));
158-
EXPECT_CALL(*m_player, load(3, dataPtr, 44100)).WillOnce(Return(true));
159-
sound->setData(3, dataPtr);
162+
EXPECT_CALL(*m_player, load(3, data, 44100)).WillOnce(Return(true));
163+
sound->setData(3, data);
160164

161165
auto clonePlayer = std::make_shared<AudioPlayerMock>();
162166
EXPECT_CALL(m_playerFactory, createAudioPlayer()).WillOnce(Return(clonePlayer));

0 commit comments

Comments
 (0)