Skip to content

Commit ce8d400

Browse files
authored
Merge pull request #2369 from jepler/nrf-usb-crash
Fix crashes while copying files to nRF via USB
2 parents e5adf3c + 6305d48 commit ce8d400

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

py/gc.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,9 @@ void gc_free(void *ptr) {
667667
if (ptr == NULL) {
668668
GC_EXIT();
669669
} else {
670+
if (MP_STATE_MEM(gc_pool_start) == 0) {
671+
reset_into_safe_mode(GC_ALLOC_OUTSIDE_VM);
672+
}
670673
// get the GC block number corresponding to this pointer
671674
assert(VERIFY_PTR(ptr));
672675
size_t block = BLOCK_FROM_PTR(ptr);

supervisor/shared/external_flash/external_flash.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ static bool allocate_ram_cache(void) {
324324
return true;
325325
}
326326

327+
if (MP_STATE_MEM(gc_pool_start) == 0) {
328+
return false;
329+
}
330+
327331
MP_STATE_VM(flash_ram_cache) = m_malloc_maybe(blocks_per_sector * pages_per_block * sizeof(uint32_t), false);
328332
if (MP_STATE_VM(flash_ram_cache) == NULL) {
329333
return false;
@@ -367,7 +371,7 @@ static void release_ram_cache(void) {
367371
if (supervisor_cache != NULL) {
368372
free_memory(supervisor_cache);
369373
supervisor_cache = NULL;
370-
} else {
374+
} else if (MP_STATE_MEM(gc_pool_start)) {
371375
m_free(MP_STATE_VM(flash_ram_cache));
372376
}
373377
MP_STATE_VM(flash_ram_cache) = NULL;
@@ -415,7 +419,7 @@ static bool flush_ram_cache(bool keep_cache) {
415419
write_flash(current_sector + (i * pages_per_block + j) * SPI_FLASH_PAGE_SIZE,
416420
MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j],
417421
SPI_FLASH_PAGE_SIZE);
418-
if (!keep_cache && supervisor_cache == NULL) {
422+
if (!keep_cache && supervisor_cache == NULL && MP_STATE_MEM(gc_pool_start)) {
419423
m_free(MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j]);
420424
}
421425
}

0 commit comments

Comments
 (0)