Skip to content

Commit 2c1d3f2

Browse files
authored
Fix SDLNet Initialisation and error path (#3)
* Initialize RawSocketSerial::thread_active to false so that it doesn't try to cleanup uninitialized objects on error. * Reorder main to avoid thread race conditions involving pin_map if listen_on_port fails * Initialize SDL and SDLNet so that socket can be opened
1 parent 0e047cc commit 2c1d3f2

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/MarlinSimulator/RawSocketSerial.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class RawSocketSerial {
3737
}
3838
start_listen(ip);
3939
server_thread = std::thread(&RawSocketSerial::execute, this);
40+
thread_active = true;
4041
}
4142

4243
void stop() {
@@ -178,7 +179,7 @@ class RawSocketSerial {
178179
void write(const char* str) { while (*str) tx_buffer.write(*str++); }
179180
void write(const uint8_t* buffer, size_t size) { tx_buffer.write((uint8_t *)buffer, size); }
180181

181-
bool thread_active = true;
182+
bool thread_active = false;
182183
ServerInfo server{};
183184
uint8_t receive_buffer[ServerInfo::max_packet_size];
184185
uint8_t transmit_buffer[ServerInfo::max_packet_size];

src/MarlinSimulator/main.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,16 @@ void simulation_main() {
6060

6161
// Main code
6262
int main(int, char**) {
63-
Application app;
64-
std::thread simulation_loop(simulation_main);
63+
SDL_Init(0);
64+
SDLNet_Init();
6565

66+
// Listen before starting simulator loop to avoid
67+
// thread synchronization issues if listen_on_port fails
6668
net_serial.listen_on_port(8099);
6769

70+
Application app;
71+
std::thread simulation_loop(simulation_main);
72+
6873
while (app.active) {
6974
app.update();
7075
app.render();
@@ -76,5 +81,8 @@ int main(int, char**) {
7681
simulation_loop.join();
7782
net_serial.stop();
7883

84+
SDLNet_Quit();
85+
SDL_Quit();
86+
7987
return 0;
8088
}

0 commit comments

Comments
 (0)