Lua:unmapMemory

From Cheat Engine
Revision as of 23:39, 26 June 2026 by Leunsel (talk | contribs) (Added related function template.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

<> Lua API Reference

function unmapMemory(address, mdl) : void

Unmaps a memory region that was previously mapped with mapMemory.

The mdl value must be the MDL returned by mapMemory.

Function Parameters

Parameter Type Description
address Integer The mapped address returned by mapMemory.
mdl Integer The MDL handle returned by mapMemory.

Returns

This function does not return a value.

Examples

Map and unmap memory

 1 local address = getAddress("game.exe+1234")
 2 local size = 4096
 3 local frompid = getOpenedProcessID()
 4 
 5 local mappedAddress, mdl = mapMemory(address, size, frompid, 0)
 6 
 7 if mappedAddress ~= nil and mappedAddress ~= 0 then
 8   print(string.format("Mapped address: %X", mappedAddress))
 9 end
10 
11 unmapMemory(mappedAddress, mdl)

Validate values before unmapping

 1 local address = getAddress("game.exe+1234")
 2 local size = 4096
 3 local frompid = getOpenedProcessID()
 4 
 5 local mappedAddress, mdl = mapMemory(address, size, frompid, 0)
 6 
 7 if mappedAddress ~= nil and mappedAddress ~= 0 and mdl ~= nil and mdl ~= 0 then
 8   unmapMemory(mappedAddress, mdl)
 9 else
10   print("Mapping failed or returned invalid values")
11 end

Read from mapped memory and clean up

 1 local address = getAddress("game.exe+1234")
 2 local size = 4096
 3 local frompid = getOpenedProcessID()
 4 
 5 local mappedAddress, mdl = mapMemory(address, size, frompid, 0)
 6 
 7 if mappedAddress ~= nil and mappedAddress ~= 0 then
 8   local value = readInteger(mappedAddress)
 9 
10   print("Value: " .. tostring(value))
11 end
12 
13 unmapMemory(mappedAddress, mdl)

Use pcall to ensure unmapMemory is called

 1 local address = getAddress("game.exe+1234")
 2 local size = 4096
 3 local frompid = getOpenedProcessID()
 4 
 5 local mappedAddress, mdl = mapMemory(address, size, frompid, 0)
 6 
 7 local ok, err = pcall(function()
 8   if mappedAddress ~= nil and mappedAddress ~= 0 then
 9     print(readInteger(mappedAddress))
10   end
11 end)
12 
13 if mappedAddress ~= nil and mappedAddress ~= 0 and mdl ~= nil and mdl ~= 0 then
14   unmapMemory(mappedAddress, mdl)
15 end
16 
17 if not ok then
18   print(err)
19 end

Store mapping information for later cleanup

 1 local mapping = {}
 2 
 3 mapping.address = getAddress("game.exe+1234")
 4 mapping.size = 4096
 5 mapping.mappedAddress, mapping.mdl = mapMemory(mapping.address, mapping.size, getOpenedProcessID(), 0)
 6 
 7 -- Later:
 8 if mapping.mappedAddress ~= nil and mapping.mappedAddress ~= 0 and mapping.mdl ~= nil and mapping.mdl ~= 0 then
 9   unmapMemory(mapping.mappedAddress, mapping.mdl)
10 
11   mapping.mappedAddress = nil
12   mapping.mdl = nil
13 end

Unmap multiple mapped regions

 1 local mappings = {}
 2 
 3 mappings[#mappings + 1] = {mapMemory(getAddress("game.exe+1000"), 4096, getOpenedProcessID(), 0)}
 4 mappings[#mappings + 1] = {mapMemory(getAddress("game.exe+2000"), 4096, getOpenedProcessID(), 0)}
 5 
 6 for i, mapping in ipairs(mappings) do
 7   local mappedAddress = mapping[1]
 8   local mdl = mapping[2]
 9 
10   if mappedAddress ~= nil and mappedAddress ~= 0 and mdl ~= nil and mdl ~= 0 then
11     unmapMemory(mappedAddress, mdl)
12   end
13 end

Main Pages

Core Lua documentation entry points

Lua
Script Engine

Sections / Views

Memory Mapping

Memory Copy / Compare

Memory Streams