diff --git a/src/target/lpc43xx.c b/src/target/lpc43xx.c index d09bb211bd2..98771b1d7fc 100644 --- a/src/target/lpc43xx.c +++ b/src/target/lpc43xx.c @@ -680,10 +680,12 @@ static bool lpc43x0_attach(target_s *const target) static void lpc43x0_detach(target_s *const target) { lpc43x0_priv_s *const priv = (lpc43x0_priv_s *)target->target_storage; - free(priv->flash->flash_high); - free(priv->flash); - priv->flash = NULL; - target->flash = NULL; + if (priv->flash) { + free(priv->flash->flash_high); + free(priv->flash); + priv->flash = NULL; + target->flash = NULL; + } cortexm_detach(target); }