Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete Zone and Group Memory Error #1135

Merged
merged 2 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src-ui/components/MultiScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ void MultiScreen::onOtherTabSelection()
if (pgz.empty())
{
}
if (pgz == "part")
else if (pgz == "part")
parts->setSelectedTab(0);
else if (pgz == "group")
parts->setSelectedTab(1);
Expand Down
16 changes: 15 additions & 1 deletion src/messaging/audio/audio_serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ enum AudioToSerializationMessageId
a2s_structure_refresh,
a2s_processor_refresh,
a2s_macro_updated,
a2s_delete_this_pointer,
};

/**
Expand All @@ -64,13 +65,25 @@ struct AudioToSerialization

// std::variant does not allow an array type and
// i kinda want this fixed size anyway
struct ToBeDeleted
{
void *ptr;
enum uint16_t
{
engine_Zone,
engine_Group,
} type;
};

static_assert(sizeof(ToBeDeleted) < 8 * sizeof(int32_t));
union Payload
{
int32_t i[8];
uint32_t u[8];
float f[8];
char c[32];
void *p;
ToBeDeleted delThis;
} payload{};

enum PayloadType : uint8_t
Expand All @@ -80,7 +93,8 @@ struct AudioToSerialization
UINT,
FLOAT,
CHAR,
VOID_STAR
VOID_STAR,
TO_BE_DELETED
} payloadType{INT};
};

Expand Down
61 changes: 27 additions & 34 deletions src/messaging/client/structure_messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,22 @@ CLIENT_TO_SERIAL(CreateGroup, c2s_create_group, int, createGroupIn(payload, engi
inline void removeZone(const selection::SelectionManager::ZoneAddress &a, engine::Engine &engine,
MessageController &cont)
{
engine::Zone *zoneToFree{nullptr};
cont.scheduleAudioThreadCallbackUnderStructureLock(
[s = a, &zoneToFree](auto &e) {
[s = a](auto &e) {
auto &zoneO = e.getPatch()->getPart(s.part)->getGroup(s.group)->getZone(s.zone);
e.terminateVoicesForZone(*zoneO);
auto zid = zoneO->id;
auto z = e.getPatch()->getPart(s.part)->getGroup(s.group)->removeZone(zid);
zoneToFree = z.release();
auto zoneToFree = z.release();

messaging::audio::AudioToSerialization a2s;
a2s.id = audio::a2s_delete_this_pointer;
a2s.payloadType = audio::AudioToSerialization::TO_BE_DELETED;
a2s.payload.delThis.ptr = zoneToFree;
a2s.payload.delThis.type = audio::AudioToSerialization::ToBeDeleted::engine_Zone;
e.getMessageController()->sendAudioToSerialization(a2s);
},
[t = a, &zoneToFree](auto &engine) {
if (zoneToFree)
{
delete zoneToFree;
}
[t = a](auto &engine) {
engine.getSampleManager()->purgeUnreferencedSamples();
engine.getSelectionManager()->guaranteeConsistencyAfterDeletes(engine);
serializationSendToClient(s2c_send_pgz_structure, engine.getPartGroupZoneStructure(),
Expand Down Expand Up @@ -215,34 +217,25 @@ CLIENT_TO_SERIAL(DeleteAllSelectedZones, c2s_delete_selected_zones, bool,
inline void removeGroup(const selection::SelectionManager::ZoneAddress &a, engine::Engine &engine,
MessageController &cont)
{
engine::Group *groupToFree{nullptr};

cont.scheduleAudioThreadCallbackUnderStructureLock(
[s = a, &groupToFree](auto &e) {
if (e.getPatch()->getPart(s.part)->getGroups().size() <= 1)
{
groupToFree = nullptr;
}
else
{
auto &groupO = e.getPatch()->getPart(s.part)->getGroup(s.group);
e.terminateVoicesForGroup(*groupO);

auto gid = e.getPatch()->getPart(s.part)->getGroup(s.group)->id;
groupToFree = e.getPatch()->getPart(s.part)->removeGroup(gid).release();
}
[s = a](auto &e) {
if (e.getPatch()->getPart(s.part)->getGroups().size() < s.group)
return;

auto &groupO = e.getPatch()->getPart(s.part)->getGroup(s.group);
e.terminateVoicesForGroup(*groupO);

auto gid = e.getPatch()->getPart(s.part)->getGroup(s.group)->id;
auto groupToFree = e.getPatch()->getPart(s.part)->removeGroup(gid).release();

messaging::audio::AudioToSerialization a2s;
a2s.id = audio::a2s_delete_this_pointer;
a2s.payloadType = audio::AudioToSerialization::TO_BE_DELETED;
a2s.payload.delThis.ptr = groupToFree;
a2s.payload.delThis.type = audio::AudioToSerialization::ToBeDeleted::engine_Group;
e.getMessageController()->sendAudioToSerialization(a2s);
},
[t = a, &groupToFree](auto &engine) {
if (groupToFree)
{
delete groupToFree;
}
else
{
engine.getMessageController()->reportErrorToClient(
"Unable to Remove Group", "All parts must have one group; you tried to "
"remove the last group in the part");
}
[t = a](auto &engine) {
engine.getSampleManager()->purgeUnreferencedSamples();
engine.getSelectionManager()->guaranteeConsistencyAfterDeletes(engine);

Expand Down
22 changes: 22 additions & 0 deletions src/messaging/messaging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,32 @@ void MessageController::parseAudioMessageOnSerializationThread(
}
break;
case audio::a2s_processor_refresh:
{
SCLOG("Processor Refresh Requestioned. TODO: Minimize this message "
<< (as.payload.i[0] ? "Zone" : "Group") << " slot " << as.payload.i[1]);
engine.getSelectionManager()->sendClientDataForLeadSelectionState();
break;
}
case audio::a2s_delete_this_pointer:
{
assert(as.payloadType == audio::AudioToSerialization::TO_BE_DELETED);
switch (as.payload.delThis.type)
{
case audio::AudioToSerialization::ToBeDeleted::engine_Zone:
{
auto z = (engine::Zone *)(as.payload.delThis.ptr);
delete z;
}
break;
case audio::AudioToSerialization::ToBeDeleted::engine_Group:
{
auto g = (engine::Group *)(as.payload.delThis.ptr);
delete g;
}
break;
}
}
break;
case audio::a2s_none:
break;
}
Expand Down
8 changes: 6 additions & 2 deletions src/sample/sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,17 @@ bool Sample::loadFromSF2(const fs::path &p, sf2::File *f, int presetNum, int ins
short *Sample::GetSamplePtrI16(int Channel)
{
if (bitDepth != BD_I16)
return 0;
return nullptr;
if (!sampleData[Channel])
return nullptr;
return &((short *)sampleData[Channel])[scxt::dsp::FIRoffset];
}
float *Sample::GetSamplePtrF32(int Channel)
{
if (bitDepth != BD_F32)
return 0;
return nullptr;
if (!sampleData[Channel])
return nullptr;
return &((float *)sampleData[Channel])[scxt::dsp::FIRoffset];
}

Expand Down
Loading