Skip to content

Commit 33fb270

Browse files
committed
Do not free cloned asset data
1 parent 864f74c commit 33fb270

File tree

5 files changed

+10
-1
lines changed

5 files changed

+10
-1
lines changed

include/scratchcpp/asset.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class LIBSCRATCHCPP_EXPORT Asset : public Entity
3838

3939
protected:
4040
virtual void processData(unsigned int size, void *data) { }
41+
virtual bool isClone() const { return false; }
4142

4243
private:
4344
spimpl::unique_impl_ptr<AssetPrivate> impl;

include/scratchcpp/sound.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class LIBSCRATCHCPP_EXPORT Sound : public Asset
4343

4444
protected:
4545
void processData(unsigned int size, void *data) override;
46+
virtual bool isClone() const override;
4647

4748
private:
4849
void stopCloneSounds();

src/scratch/asset.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Asset::Asset(const std::string &name, const std::string &id, const std::string &
1717
/*! Destroys Asset. */
1818
Asset::~Asset()
1919
{
20-
if (impl->data) {
20+
if (impl->data && !impl->dataCloned) {
2121
free(impl->data);
2222
impl->data = nullptr;
2323
}
@@ -68,6 +68,7 @@ void Asset::setData(unsigned int size, void *data)
6868

6969
impl->dataSize = size;
7070
impl->data = data;
71+
impl->dataCloned = isClone();
7172
processData(size, data);
7273
}
7374

src/scratch/asset_p.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ struct AssetPrivate
2121
std::string fileName;
2222
void *data = nullptr;
2323
unsigned int dataSize = 0;
24+
bool dataCloned = false;
2425
Target *target = nullptr;
2526
};
2627

src/scratch/sound.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ void Sound::processData(unsigned int size, void *data)
116116
std::cerr << "Failed to load sound " << name() << std::endl;
117117
}
118118

119+
bool Sound::isClone() const
120+
{
121+
return impl->cloneRoot;
122+
}
123+
119124
void Sound::stopCloneSounds()
120125
{
121126
Target *target = this->target();

0 commit comments

Comments
 (0)