diff --git a/java/rocksjni/ttl.cc b/java/rocksjni/ttl.cc index 4164a0c4bc..2992e930df 100644 --- a/java/rocksjni/ttl.cc +++ b/java/rocksjni/ttl.cc @@ -57,6 +57,8 @@ jobject const char* db_path = env->GetStringUTFChars(jdb_path, 0); std::vector cfnames_to_free; + // the zero-terminated version of cfnames_to_free. + std::vector c_cfnames_to_free; std::vector jcfnames_for_free; std::vector column_families; @@ -85,12 +87,17 @@ jobject rocksdb::ColumnFamilyOptionsJni::getHandle(env, jcf_opt_obj); jbyte* cfname = env->GetByteArrayElements(byteArray, 0); + const int len = env->GetArrayLength(byteArray) + 1; + char* c_cfname = new char[len]; + memcpy(c_cfname, cfname, len - 1); + c_cfname[len - 1] = 0; + // free allocated cfnames after call to open cfnames_to_free.push_back(cfname); + c_cfnames_to_free.push_back(c_cfname); jcfnames_for_free.push_back(byteArray); column_families.push_back(rocksdb::ColumnFamilyDescriptor( - reinterpret_cast(cfname), - *cfOptions)); + c_cfname, *cfOptions)); } // get iterator for TTL values iteratorObj = env->CallObjectMethod( @@ -115,6 +122,8 @@ jobject i != cfnames_to_free.size(); i++) { // free cfnames env->ReleaseByteArrayElements(jcfnames_for_free[i], cfnames_to_free[i], 0); + // free c_cfnames + delete[] c_cfnames_to_free[i]; } // check if open operation was successful @@ -167,9 +176,15 @@ jlong Java_org_rocksdb_TtlDB_createColumnFamilyWithTtl( rocksdb::ColumnFamilyOptionsJni::getHandle(env, jcf_opt_obj); jbyte* cfname = env->GetByteArrayElements(byteArray, 0); + const int len = env->GetArrayLength(byteArray) + 1; + char* c_cfname = new char[len]; + memcpy(c_cfname, cfname, len - 1); + c_cfname[len - 1] = 0; + rocksdb::Status s = db_handle->CreateColumnFamilyWithTtl( - *cfOptions, reinterpret_cast(cfname), &handle, jttl); + *cfOptions, c_cfname, &handle, jttl); env->ReleaseByteArrayElements(byteArray, cfname, 0); + delete[] c_cfname; if (s.ok()) { return reinterpret_cast(handle);