I have a memory leak in the Lua part of my application. For whatever reason, my object is not getting deleted when it should (even when I call collectgarbage("collect")). I assume this means I have a dangling reference somewhere.
So how may I obtain a list of where various references to an object reside? For example:
obj = MyObject()
ref = obj
tbl = {obj}
obj = nil
print(getreferences(obj)) -- should print something like _G.ref, _G.tbl[1]
I would simply write my own function for this, but it would not be able to find references contained inside of closures. Any advice?
There's a tool to traverse the whole Lua universe. See http://lua-users.org/lists/lua-l/2006-07/msg00110.html
Related
I would like to do something like :
var data_copy = original_data;
And then do some stuff on data_copy without modifying original_data.
Data_copy and original_data are objects.
Is there a direct way to do that in vala ?
It depends on the object. For structs, this happens automatically. For objects, there isn't a common way. Some objects have a dup() method that can do this, but it is not universal. There's no guaranteed safe way to duplicate an object since it might reference system resources or other things which cannot be duplicated.
I have a class for pieces on a board. I want to be able to delete an instance of Piece so that anything else in the program that points to that piece will just point to nil.
Here's the very basic code version of what I want to do:
piece = Piece.new
variable = piece
variable #=> <Piece:0x0000000xxxxxxxx>
piece.delete
variable #=> nil
This seems like a very basic task so I feel like I'm missing something obvious. I've tried creating a delete method for the class with "self = nil", but this returns an error ("Can't change the value of self").
So far I have just worked around this by updating the other things that point to the object in my 'delete' method, but it seems like there should be a better way.
This is not possible.
Firstly, Ruby is an object-oriented language, which means that all manipulation is done via messages to objects, and all that is manipulated are objects. Variables are not objects, therefore you cannot manipulate them. (The only things you can do with variables are assign a value to them and dereference them.)
And even if you could manipulate variables, you would still need to hunt down every single reference to the object in question and remove it, in order for the object to be eligible for "deletion" (i.e. garbage collection).
I know that I can use a With statement to make repeated references to a single object:
With myObj
.StringProperty = ""
.BooleanProperty = False
End With
However, what I want to know is: is there a shorthand for referring to the original object in the With statement? In the above example, can I refer to myObj without explicitly typing myObj as I'm already working with it.
No you can't, but it wouldn't mean much anyway. With just sets the default scope to the object expression that follows it. If you need a reference to the object this doesn't help unless the object is one of the very few that has a .Self property, which is quite rare.
I have a struct in my Ruby code that looks somewhat like this
Parameter = Struct.new(:name, :id, :default_value, :minimum, :maximum)
later, I create an instance of this struct using
freq = Parameter.new('frequency', 15, 1000.0, 20.0, 20000.0)
At some point, I need an exact duplicate of this struct, so I call
newFreq = freq.clone
Then, I change newFreq's name
newFreq.name.sub!('f', 'newF')
Which, miraculously, changes freq.name, too!
A simple assignment like newFreq.name = 'newFrequency' does not change freq.
Is this the way this is supposed to work?
Edit: Is it a good idea to use a class instead of a struct and overload clone to make a deep copy?
newFreq is a shallow copy of freq. That means that each of the references stored inside of newFreq points to the object as the ones stored in freq. You can change where the references point independantly (newFreq.name = newFreq.name.sub 'f','newF'), but if you call a method that mutates the object, both newFreq and freq will be affected.
See also http://en.wikipedia.org/wiki/Object_copy
The Object#clone method performs shallow copy. You need deep copy to get the job done.
Follow this link to learn about deep copy in Ruby.
In a VB6 application, I have a Dictionary whose keys are Strings and values are instances of a custom class. If I call RemoveAll() on the Dictionary, will it first free the custom objects? Or do I explicitly need to do this myself?
Dim d as Scripting.Dictionary
d("a") = New clsCustom
d("b") = New clsCustom
' Are these two lines necessary?
Set d("a") = Nothing
Set d("b") = Nothing
d.RemoveAll
Yes, all objects in the Dictionary will be released after a call to RemoveAll(). From a performance (as in speed) standpoint I would say those lines setting the variables to Nothing are unnecessary, because the code has to first look them up based on the key names whereas RemoveAll() will enumerate and release everything in one loop.
RemoveAll will remove all the associations from the Dictionary: both the keys and values.
It would be a reference leak for the Dictionary to keep a reference to the values in the Dictionary.
If there are no other variables that reference the items in the collection then those objects should be handed to the Garbage Collector to be cleaned up the next time the GC is run.
If you, for example do this where sObj is a static variable somewhere then the when the GC is invoked next by the system, the first object will be cleaned up but the second which still is referenced by sObj will not.
d("a") = New clsCustom
d("b") = New clsCustom code.
sObj = d("b")
d.RemoveAll()