Skip to content

Commit 1fb6b22

Browse files
authored
feat: add free_sd_images function to manage memory for C API (#1633)
1 parent c20769b commit 1fb6b22

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

include/stable-diffusion.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,10 @@ SD_API bool preprocess_canny(sd_image_t image,
491491
SD_API const char* sd_commit(void);
492492
SD_API const char* sd_version(void);
493493

494+
// for C API, caller needs to call free_sd_images to free the memory after use
495+
// This helps avoid CRT problems on Windows when memory is allocated in the library but freed in the caller, which may use a different CRT.
496+
SD_API void free_sd_images(sd_image_t* result_images, int num_images);
497+
494498
#ifdef __cplusplus
495499
}
496500
#endif

src/stable-diffusion.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5561,3 +5561,18 @@ SD_API bool generate_video(sd_ctx_t* sd_ctx,
55615561
}
55625562
return true;
55635563
}
5564+
5565+
SD_API void free_sd_images(sd_image_t* result_images, int num_images) {
5566+
if (result_images == nullptr) {
5567+
return;
5568+
}
5569+
5570+
for (int i = 0; i < num_images; ++i) {
5571+
if (result_images[i].data != nullptr) {
5572+
free(result_images[i].data);
5573+
result_images[i].data = nullptr;
5574+
}
5575+
}
5576+
5577+
free(result_images);
5578+
}

0 commit comments

Comments
 (0)