~ We like to free everything on exit, so valgrind doesn't complain (valgrind * is an awesome runtime memory usage detector for C and C++ programs). In * some ways it would be neater not to do this, but it turns out some * transient objects still need cleaning. */
| 518 | * some ways it would be neater not to do this, but it turns out some |
| 519 | * transient objects still need cleaning. */ |
| 520 | static void free_all_channels(struct lightningd *ld) |
| 521 | { |
| 522 | struct peer *p; |
| 523 | |
| 524 | /*~ tal supports *destructors* using `tal_add_destructor()`; the most |
| 525 | * common use is for an object to delete itself from a linked list |
| 526 | * when it's freed. |
| 527 | * |
| 528 | * As a result, freeing an object (which frees any tal objects |
| 529 | * allocated off it, and any allocated off them, etc) may cause |
| 530 | * callbacks; in this case, some objects freed here can cause database |
| 531 | * writes, which must be inside a transaction. */ |
| 532 | db_begin_transaction(ld->wallet->db); |
| 533 | |
| 534 | /* Now we free all the HTLCs */ |
| 535 | free_htlcs(ld, NULL); |
| 536 | |
| 537 | /*~ For every peer, we free every channel. On allocation the peer was |
| 538 | * given a destructor (`destroy_peer`) which removes itself from the |
| 539 | * list. Thus we use list_top() not list_pop() here. */ |
| 540 | while ((p = list_top(&ld->peers, struct peer, list)) != NULL) { |
| 541 | struct channel *c; |
| 542 | |
| 543 | /*~ A peer can have multiple channels; we only allow one to be |
| 544 | * open at any time, but we remember old ones for 100 blocks, |
| 545 | * after all the outputs we care about are spent. */ |
| 546 | while ((c = list_top(&p->channels, struct channel, list)) |
| 547 | != NULL) { |
| 548 | /* Removes itself from list as we free it */ |
| 549 | tal_free(c); |
| 550 | } |
| 551 | |
| 552 | /* A peer may have a channel in the process of opening. */ |
| 553 | if (p->uncommitted_channel) { |
| 554 | struct uncommitted_channel *uc = p->uncommitted_channel; |
| 555 | |
| 556 | /* Setting to NULL stops destroy_uncommitted_channel |
| 557 | * from trying to remove peer from db! */ |
| 558 | p->uncommitted_channel = NULL; |
| 559 | tal_free(uc); |
| 560 | } |
| 561 | /* Removes itself from list as we free it */ |
| 562 | tal_free(p); |
| 563 | } |
| 564 | |
| 565 | /*~ Commit the transaction. Note that the db is actually |
| 566 | * single-threaded, so commits never fail and we don't need |
| 567 | * spin-and-retry logic everywhere. */ |
| 568 | db_commit_transaction(ld->wallet->db); |
| 569 | } |
| 570 | |
| 571 | static void shutdown_global_subdaemons(struct lightningd *ld) |
| 572 | { |
no test coverage detected