Skip to content

Commit

Permalink
Make sure the temporary frames array is freed
Browse files Browse the repository at this point in the history
  • Loading branch information
jbachorik committed Apr 9, 2024
1 parent 88819f6 commit c7a9781
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
11 changes: 6 additions & 5 deletions ddprof-lib/src/main/cpp/livenessTracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,21 @@ void LivenessTracker::cleanup_table(bool forced) {
u32 sz, newsz = 0;
std::set<jclass> kept_classes;
for (u32 i = 0; i < (sz = _table_size); i++) {
if (_table[i].ref != NULL && !env->IsSameObject(_table[i].ref, NULL)) {
if (_table[i].ref != nullptr && !env->IsSameObject(_table[i].ref, nullptr)) {
// it survived one more GarbageCollectionFinish event
u32 target = newsz++;
if (target != i) {
_table[target] = _table[i];
_table[i].ref = NULL;
_table[i].ref = nullptr;
assert(_table[i].frames == _table[target].frames);
_table[i].frames = nullptr;
}
_table[target].age += epoch_diff;
} else {
env->DeleteWeakGlobalRef(_table[i].ref);
_table[i].ref = NULL;
_table[i].ref = nullptr;
delete[] _table[i].frames;
_table[i].frames = nullptr;
}
}

Expand Down Expand Up @@ -296,8 +299,6 @@ void LivenessTracker::track(JNIEnv* env, AllocEvent &event, jint tid, jobject ob
}
}
}

delete[] frames;
}

void JNICALL LivenessTracker::GarbageCollectionFinish(jvmtiEnv *jvmti_env) {
Expand Down
7 changes: 3 additions & 4 deletions ddprof-lib/src/main/cpp/objectSampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,11 @@ void ObjectSampler::recordAllocation(jvmtiEnv* jvmti, JNIEnv* jni, jthread threa
}

if (_record_liveness) {
// 'frames' will be released by the tracker
LivenessTracker::instance()->track(jni, event, tid, object, frames_size, frames);
} else {
// otherwise the 'frames' need to be deleted here
delete[] frames;
}

// it's safe to delete frames - the liveness tracker keeps a full copy of the frames and manages its own memory
delete[] frames;
}

Error ObjectSampler::check(Arguments& args) {
Expand Down

0 comments on commit c7a9781

Please sign in to comment.