ML: Reserve cluster centers (#21795)
Reserve cluster centers to prevent heap-use-after-free in k-means training
Stelios Fragkakis committed
Feb 21, 2026 at 18:07 UTC
00b789eeb0968dbbf9830ecd962c9337cb5c6988
1 file changed
+1
-1
src/ml/ml_kmeans.cc
+1
-1
@@ -41,7 +41,7 @@ ml_kmeans_train(ml_kmeans_t *kmeans, const ml_features_t *features, unsigned max
41
// reallocation during lazy evaluation. dlib uses expression templates that hold
42
// references to vector elements, and reallocation would invalidate those references,
43
// causing heap-use-after-free when multiple threads train models concurrently.
44
- //kmeans->cluster_centers.reserve(2);
44
+ kmeans->cluster_centers.reserve(2);
45
46
dlib::pick_initial_centers(2, kmeans->cluster_centers, features->preprocessed_features);
47
dlib::find_clusters_using_kmeans(features->preprocessed_features, kmeans->cluster_centers, max_iters);