3

According to this

Vala doesn't have garbage collection. It does reference counting.

I'm not exactly sure what the difference is between garbage collection and reference counting nor could I find a clear explanation. Do I explicitly need to delete bmp and/or add a destructor to class Bmp? IOW: does this code have a memory leak?

public void* run() {
    while(true) {
        if(detected) {
            ...
            var bmp = new Bmp(800,800);
            ...

public class Bmp {
    ...

1 Answer 1

7

The code you have written is not a memory leak, but it is possible to write one. For instance:

   class Foo {
       Foo? f;
   }
   var foo1 = new Foo();
   var foo2 = new Foo();
   foo1.f = foo2;
   foo2.f = foo1;

Every time a Foo is assigned, a counter for that instance is incremented and decremented when it is unassigned. Because foo1 and foo2 have references to each other, their counts will never go to zero, even if there are no references to them in the rest of the program. The unowned keyword makes a reference that not counted. So, a dangling pointer can be written as follows:

var foo = new Foo();
unowned Foo f = foo;
foo = null;
// f now refers to an object that has been deallocated.

If your object graph has no cycles, there will be no problem. If you have any circular references, you may leak memory if you do not clean up properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.