Tencent/matrix · error · IllegalStateException

Path $absolutePath is pointed to an existing element but it…

Error message

Path $absolutePath is pointed to an existing element but it is not a directory.

What it means

The File.assureIsDirectory extension in MemoryUtil throws IllegalStateException when the path exists but is a regular file (or other non-directory element) instead of creating the directory. It is used to guarantee the hprof/leak output directory is valid before writing.

Solutions

  1. Delete the conflicting file at that path so the directory can be created: File(path).delete() before invoking the API.
  2. Choose a different, dedicated directory path for Matrix resource-canary output.
  3. On startup, ensure the path is a directory (mkdirs) before enabling leak dumping.

Example fix

// before
val out = File(context.filesDir, "hprof") // some prior run created a FILE named 'hprof'
MemoryUtil.parse(out, ...)
// after
val out = File(context.filesDir, "hprof")
if (out.exists() && !out.isDirectory) out.delete()
out.mkdirs()
MemoryUtil.parse(out, ...)
Defensive patterns

Strategy: validation

Validate before calling

fun assureOutputDir(path: File): File {
  if (path.exists() && !path.isDirectory) path.delete()
  if (!path.isDirectory) path.mkdirs()
  return path
}

Type guard

fun File.isUsableDir(): Boolean = isDirectory || (!exists() && parentFile?.canWrite() == true)

Try / catch

try {
  MemoryUtil.parse(dir, ...)
} catch (e: IllegalStateException) {
  MatrixLog.e(TAG, "output path invalid: ${e.message}")
  dir.delete(); dir.mkdirs()
}

Prevention

When it happens

Trigger: Calling MemoryUtil APIs (e.g. parsing a leak chain or dumping hprof metadata) with an output directory path that currently points to an existing file.

Common situations: A stale file occupies the intended directory path (previous crash wrote a file there); user configured a cache dir path that collides with a file; sync tools replaced the directory with a file.

Related errors


AI-assisted analysis of Tencent/matrix@3b8293bd65 (2026-09-08). Data as JSON: /api/errors/d267c648d44e29bb. Report an issue: GitHub.

Appendix: source

Thrown at matrix/matrix-android/matrix-resource-canary/matrix-resource-canary-android/src/main/java/com/tencent/matrix/resource/MemoryUtil.kt:37

private fun info(message: String) {
    MatrixLog.i(TAG, message)
}

private fun error(message: String, throwable: Throwable? = null) {
    if (throwable != null)
        MatrixLog.printErrStackTrace(TAG, throwable, message)
    else
        MatrixLog.e(TAG, message)
}

private val currentTime: Long
    get() = System.currentTimeMillis()

private fun File.assureIsDirectory() {
    if (!isDirectory) {
        if (exists())
            throw IllegalStateException("Path $absolutePath is pointed to an existing element but it is not a directory.")
        mkdirs()
    }
}

private class OrderedStreamWrapper(
    private val order: ByteOrder,
    private val stream: InputStream
) {

    fun readOrderedInt(): Int {
        val buffer = ByteBuffer.allocate(4)
            .apply {
                order(order)
            }
        stream.read(buffer.array(), 0, 4)
        return buffer.getInt(0)
    }

View on GitHub (pinned to 3b8293bd65)