Skip to content

Commit 1474097

Browse files
committed
free memory allocated for results
1 parent 92a769d commit 1474097

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

src/gc-alloc-profiler.cpp

+14-5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ struct AllocProfile {
3838
// == global variables manipulated by callbacks ==
3939

4040
AllocProfile g_alloc_profile;
41+
RawAllocResults *g_alloc_profile_results = nullptr;
4142
int g_alloc_profile_enabled = false;
4243

4344
// === stack stuff ===
@@ -63,25 +64,27 @@ JL_DLLEXPORT void jl_start_alloc_profile(int skip_every) {
6364

6465
extern "C" { // Needed since the function doesn't take any arguments.
6566

66-
JL_DLLEXPORT struct RawAllocResults jl_stop_alloc_profile() {
67+
JL_DLLEXPORT struct RawAllocResults* jl_stop_alloc_profile() {
6768
g_alloc_profile_enabled = false;
6869

69-
auto results = RawAllocResults{
70+
auto results = new RawAllocResults{
7071
g_alloc_profile.allocs.data(),
7172
g_alloc_profile.allocs.size()
7273
};
7374

7475
// package up frees
75-
results.num_frees = g_alloc_profile.frees_by_type_address.size();
76-
results.frees = (FreeInfo*) malloc(sizeof(FreeInfo) * results.num_frees);
76+
results->num_frees = g_alloc_profile.frees_by_type_address.size();
77+
results->frees = (FreeInfo*) malloc(sizeof(FreeInfo) * results->num_frees);
7778
int j = 0;
7879
for (auto type_addr_free_count : g_alloc_profile.frees_by_type_address) {
79-
results.frees[j++] = FreeInfo{
80+
results->frees[j++] = FreeInfo{
8081
type_addr_free_count.first,
8182
type_addr_free_count.second
8283
};
8384
}
8485

86+
g_alloc_profile_results = results;
87+
8588
return results;
8689
}
8790

@@ -93,6 +96,12 @@ JL_DLLEXPORT void jl_free_alloc_profile() {
9396
free(alloc.backtrace.data);
9497
}
9598
g_alloc_profile.allocs.clear();
99+
100+
if (g_alloc_profile_results != nullptr) {
101+
free(g_alloc_profile_results->frees);
102+
// free the results?
103+
g_alloc_profile_results = nullptr;
104+
}
96105
}
97106

98107
}

src/gc-alloc-profiler.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void _report_gc_finished(
3434
uint64_t pause, uint64_t freed, uint64_t allocd, int full, int recollect
3535
) JL_NOTSAFEPOINT;
3636
JL_DLLEXPORT void jl_start_alloc_profile(int skip_every);
37-
JL_DLLEXPORT struct RawAllocResults jl_stop_alloc_profile(void);
37+
JL_DLLEXPORT struct RawAllocResults *jl_stop_alloc_profile(void);
3838
JL_DLLEXPORT void jl_free_alloc_profile(void);
3939

4040
void _record_allocated_value(jl_value_t *val, size_t size) JL_NOTSAFEPOINT;

stdlib/AllocProfile/src/AllocProfile.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ function start(skip_every::Int=0)
4242
end
4343

4444
function stop()
45-
raw_results = ccall(:jl_stop_alloc_profile, RawAllocResults, ())
45+
raw_results_ptr = ccall(:jl_stop_alloc_profile, Ptr{RawAllocResults}, ())
46+
raw_results = unsafe_load(raw_results_ptr)
4647
decoded_results = decode(raw_results)
4748
return decoded_results
4849
end

stdlib/AllocProfile/test/runtests.jl

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ using AllocProfile
1212
using Base64
1313

1414
results = AllocProfile.stop()
15+
AllocProfile.clear()
1516

1617
@test length(results.allocs) > 0
1718
first_alloc = results.allocs[1]

0 commit comments

Comments
 (0)