Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Question about properly disposing of MemScan and FoundList

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Kamd
Cheater
Reputation: 1

Joined: 02 May 2018
Posts: 28

PostPosted: Tue Apr 09, 2019 7:14 pm    Post subject: Question about properly disposing of MemScan and FoundList Reply with quote

I made a helper (short and easy to use) function for scanning string values in memory.

Basically, creates MemScan and does firstScan, initializes FoundList and returns the FoundList so I can store it in a variable in my main code.

I have read I need to destroy everything when I'm done using it.
Is the following code going to dispose everything correctly? (I'm worried that the local scoped 'f' variable in scan function won't be disposed?)

Code:

function strScan(find)
    local ms = createMemScan()

    ms.firstScan(...)

    ms.waitTillDone()

    local f = createFoundList(ms)
    f.initialize()

    ms.destroy()
    return f
end

local someStrings = strScan("hello world")

-- now I use someStrings etc
local strAddr = someStrings[0]

-- and now I'm done, dispose it?
someStrings.destroy()



EDIT: For clarification, I don't need to manually destroy readString variables? e.g:
Code:

function example()
  local str = readString("some address")
  -- do stuff..
  -- do I have to str.destroy() or will this automatically be garbage collected since its only scoped to this function?
end


Thank you for any feedback.


Last edited by Kamd on Thu Apr 11, 2019 4:34 am; edited 1 time in total
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 152

Joined: 06 Jul 2014
Posts: 4699

PostPosted: Tue Apr 09, 2019 7:55 pm    Post subject: Reply with quote

readString returns a Lua string. CE does not manage it at all; Lua will garbage collect the string when it's no longer used.

You should destroy the foundlist eventually. Ideally, strScan should do that. Copy the foundlist into a Lua array and return that instead.

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Kamd
Cheater
Reputation: 1

Joined: 02 May 2018
Posts: 28

PostPosted: Thu Apr 11, 2019 4:44 am    Post subject: Reply with quote

ParkourPenguin wrote:
You should destroy the foundlist eventually. Ideally, strScan should do that. Copy the foundlist into a Lua array and return that instead.


My bad, my example code I accidentally did strAddr.destroy() instead of the correct someStrings.destroy() - I have updated the original post

So does the FoundList have to be destroyed directly in the strScan function, or is it enough to destroy the variable holding the returned FoundList?
Code:

local myScan = strScan("some string")
myScan.destroy()


If I have to destroy the FoundList (f) directly in the strScan function, would this be the correct way to copy it to an array?
Code:

local copy = { f }
f.destroy()
return copy[1]


Thanks
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 152

Joined: 06 Jul 2014
Posts: 4699

PostPosted: Thu Apr 11, 2019 8:48 am    Post subject: Reply with quote

No, the foundlist doesn't have to be destroyed in the strScan function; it's just better design IMO. Users of the function don't have to remember to destroy it every time they use it (less code duplication; less chance of memory leak), and you can use it like any other Lua array (i.e. it starts at 1; # operator works).
Code:
--...
local results={}
for i = 0, f.Count-1 do
  results[i+1] = getAddress(f[i])
end     
f.destroy()
ms.destroy()
return results

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Kamd
Cheater
Reputation: 1

Joined: 02 May 2018
Posts: 28

PostPosted: Thu Apr 11, 2019 8:17 pm    Post subject: Reply with quote

ParkourPenguin wrote:
Users of the function don't have to remember to destroy it every time they use it (less code duplication; less chance of memory leak)


Great idea, I will definitely use this method.

getAddress: I read that it returns integer, but I would need the normal string address, would it be fine to store f[i] instead of getAddress[f[i])?

Just some followup questions for curiosity:
    - Is there any case where I would actually need the FoundList object rather than the addresses
    - Will I never have to destroy the returned results array? (since Lua will garbage collect it automatically?)
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 152

Joined: 06 Jul 2014
Posts: 4699

PostPosted: Thu Apr 11, 2019 9:05 pm    Post subject: Reply with quote

Sure, it's fine to copy the string. I'm usually doing arithmetic with addresses, but if you don't need to, there's little point in turning them into integers.

As far as I can tell, there's no need to keep the foundlist around. It might do something weird with values, but you can read the current value as whatever type you want whenever (and without needing to clean up after it).

Lua will automatically garbage collect that array when it's no longer used. You don't have to do anything to clean it up.

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites