Skip to content

Commit 5ebc345

Browse files
committed
Possible bugfix for wled#3609 wled#3616
1 parent b743ca8 commit 5ebc345

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

wled00/FX_fcn.cpp

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,21 @@ Segment::Segment(const Segment &orig) {
9090
//DEBUG_PRINTF("-- Copy segment constructor: %p -> %p\n", &orig, this);
9191
memcpy((void*)this, (void*)&orig, sizeof(Segment));
9292
_t = nullptr; // copied segment cannot be in transition
93-
if (orig.name) { name = new char[strlen(orig.name)+1]; if (name) strcpy(name, orig.name); } else { name = nullptr; }
94-
if (orig.data) { if (allocateData(orig._dataLen)) memcpy(data, orig.data, orig._dataLen); } else { data = nullptr; _dataLen = 0; }
93+
name = nullptr;
94+
data = nullptr;
95+
_dataLen = 0;
96+
if (orig.name) { name = new char[strlen(orig.name)+1]; if (name) strcpy(name, orig.name); }
97+
if (orig.data) { if (allocateData(orig._dataLen)) memcpy(data, orig.data, orig._dataLen); }
9598
}
9699

97100
// move constructor
98101
Segment::Segment(Segment &&orig) noexcept {
99102
//DEBUG_PRINTF("-- Move segment constructor: %p -> %p\n", &orig, this);
100103
memcpy((void*)this, (void*)&orig, sizeof(Segment));
104+
orig._t = nullptr; // old segment cannot be in transition any more
101105
orig.name = nullptr;
102106
orig.data = nullptr;
103107
orig._dataLen = 0;
104-
orig._t = nullptr; // old segment cannot be in transition any more
105108
}
106109

107110
// copy assignment
@@ -110,21 +113,15 @@ Segment& Segment::operator= (const Segment &orig) {
110113
if (this != &orig) {
111114
// clean destination
112115
if (name) { delete[] name; name = nullptr; }
113-
if (orig.name) { name = new char[strlen(orig.name)+1]; if (name) strcpy(name, orig.name); }
114-
if (_t) {
115-
#ifndef WLED_DISABLE_MODE_BLEND
116-
if (_t->_segT._dataT) free(_t->_segT._dataT);
117-
#endif
118-
delete _t;
119-
_t = nullptr; // copied segment cannot be in transition
120-
}
116+
stopTransition();
121117
deallocateData();
122118
// copy source
123119
memcpy((void*)this, (void*)&orig, sizeof(Segment));
124120
// erase pointers to allocated data
125121
data = nullptr;
126122
_dataLen = 0;
127123
// copy source data
124+
if (orig.name) { name = new char[strlen(orig.name)+1]; if (name) strcpy(name, orig.name); }
128125
if (orig.data) { if (allocateData(orig._dataLen)) memcpy(data, orig.data, orig._dataLen); }
129126
}
130127
return *this;
@@ -135,13 +132,7 @@ Segment& Segment::operator= (Segment &&orig) noexcept {
135132
//DEBUG_PRINTF("-- Moving segment: %p -> %p\n", &orig, this);
136133
if (this != &orig) {
137134
if (name) { delete[] name; name = nullptr; } // free old name
138-
if (_t) {
139-
#ifndef WLED_DISABLE_MODE_BLEND
140-
if (_t->_segT._dataT) free(_t->_segT._dataT);
141-
#endif
142-
delete _t;
143-
_t = nullptr;
144-
}
135+
stopTransition();
145136
deallocateData(); // free old runtime data
146137
memcpy((void*)this, (void*)&orig, sizeof(Segment));
147138
orig.name = nullptr;
@@ -153,10 +144,13 @@ Segment& Segment::operator= (Segment &&orig) noexcept {
153144
}
154145

155146
bool Segment::allocateData(size_t len) {
156-
if (data && _dataLen == len) return true; //already allocated
147+
if (data && _dataLen >= len) { // already allocated enough (reduce fragmentation)
148+
if (call == 0) memset(data, 0, len); // erase buffer if called during effect initialisation
149+
return true;
150+
}
157151
//DEBUG_PRINTF("-- Allocating data (%d): %p\n", len, this);
158152
deallocateData();
159-
if (len == 0) return(false); // nothing to do
153+
if (len == 0) return false; // nothing to do
160154
if (Segment::getUsedSegmentData() + len > MAX_SEGMENT_DATA) {
161155
// not enough memory
162156
DEBUG_PRINT(F("!!! Effect RAM depleted: "));

0 commit comments

Comments
 (0)