{"record":{"id":"6e1195567f02d976","repo":"dotnet/runtime","slug":"value-must-be-an-address-in-the-managed-heap","errorCode":null,"errorMessage":"value must be an address in the managed heap","messagePattern":"value must be an address in the managed heap","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/mono/browser/runtime/roots.ts","lineNumber":86,"sourceCode":" * The result object has get() and set(value) methods, along with a .value property.\n * When you are done using the root you must call its .release() method.\n */\nexport function mono_wasm_new_root<T extends MonoObject> (value: T | undefined = undefined): WasmRoot<T> {\n    if (WasmEnableThreads && runtimeHelpers.disableManagedTransition) throw new Error(\"External roots are not supported in multithreaded mode\");\n    let result: WasmRoot<T>;\n\n    if (_scratch_root_free_instances.length > 0) {\n        result = _scratch_root_free_instances.pop()!;\n    } else {\n        const index = _mono_wasm_claim_scratch_index();\n        const buffer = _scratch_root_buffer;\n\n        result = new WasmJsOwnedRoot(buffer!, index);\n    }\n\n    if (value !== undefined) {\n        if (typeof (value) !== \"number\")\n            throw new Error(\"value must be an address in the managed heap\");\n\n        result.set(value);\n    } else {\n        result.set(<any>0);\n    }\n\n    return result;\n}\n\n/**\n * Allocates 1 or more temporary roots, accepting either a number of roots or an array of pointers.\n * mono_wasm_new_roots(n): returns an array of N zero-initialized roots.\n * mono_wasm_new_roots([a, b, ...]) returns an array of new roots initialized with each element.\n * Each root must be released with its release method, or using the mono_wasm_release_roots API.\n */\nexport function mono_wasm_new_roots<T extends MonoObject> (count_or_values: number | T[]): WasmRoot<T>[] {\n    let result;\n","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/dotnet/runtime/blob/60108ba66eb7d1d12f595480091b4ad80a24b172/src/mono/browser/runtime/roots.ts#L68-L104","documentation":"Thrown by mono_wasm_new_root when an initial value is supplied that is not undefined and not a number. Managed-heap addresses are represented as numeric pointers; passing an object, string, or other type as the initial value cannot be stored as a managed pointer, so it is rejected.","triggerScenarios":"Calling mono_wasm_new_root(someObject), mono_wasm_new_root('a string'), or any call where the second argument is defined but typeof value !== 'number'. Passing undefined is allowed (zero-initializes the root).","commonSituations":"Accidentally passing a JS wrapper or WasmRoot instance instead of its .value/.address; a refactor that changed the parameter type without updating call sites; passing a managed object handle object rather than its numeric id.","solutions":["Pass a numeric managed-heap address, or pass undefined for zero-initialization.","If you have a WasmRoot, use root.value or root.address to get the numeric pointer first.","Add a typeof check at the call site to fail with a clearer upstream message."],"exampleFix":"// before\nconst r = mono_wasm_new_root(otherRoot); // otherRoot is an object\n// after\nconst r = mono_wasm_new_root(otherRoot.value); // numeric pointer","handlingStrategy":"type-guard","validationCode":"function newRootSafe(value?: number) {\n  if (value !== undefined && typeof value !== 'number') {\n    throw new TypeError('initial value must be a numeric managed pointer or undefined');\n  }\n  return mono_wasm_new_root(value);\n}","typeGuard":"function isManagedPointer(v: unknown): v is number {\n  return v === undefined || (typeof v === 'number' && Number.isFinite(v));\n}","tryCatchPattern":null,"preventionTips":["Pass undefined rather than 0 for explicit zero-initialization if unsure.","When you already hold a root, dereference with .value before forwarding.","Type call sites strictly as (value?: number)."],"tags":["gc-roots","validation","argument-error","typescript"],"backgroundTag":null,"analyzedSha":"60108ba66eb7d1d12f595480091b4ad80a24b172","analyzedAt":"2026-08-10T18:54:11.478Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}