PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Freeing objects 2

What if I create a list of objects? How do I free this?

for(i$,0,12)
#object Card testcard$(i,i+1);
CardList%[i$] = &testcard;$;
end;

I have twelve objects in a list. Do I free the list? if so, how?
Try not to use the % anywhere. Use the $ and make the variable global with the Global() function.

To free the objects, simply do this:

Code:

Card(testcard$);
for (i$, 0, 12)
&testcard;$ = CardList$[i$];
free(testcard$);
end;


That's it. PPL needs to know you are freeing an object variable to free the object. If you just do a free(CardList$[i$]); you are freeing a non-object address.

In the next version of PPL (yes I made a change again) you will be able to use the FreeObject(CardList$[i$]) function. It will take an object address only.
Here, this will work well with the next version of PPL:

Code:
#class myclass
public(value$);

nproc create
value$ = args$[0];
end;

proc destroy
ShowMessage("Destroy " + value$);
end;
#endclass

proc main
dim(l$, 20);
for (i$, 1, 10)
#object myclass o$(i$);
l$[i$] = &o;$;
end;
for (i$, 1, 10)
freeobject(l$[i$]);
end;
end;
Man, I have probably created quite the memory leaks before asking this. I had made 85 objects with the FOR statement and assigned them to a list. However I never freed them.
Hehe, yep quite a memory leak.

Since the new version might not be out until the end of next week. Use this method to free objects from an array:

Code:
Card(testcard$);
for (i$, 0, 12)
&testcard;$ = CardList$[i$];
free(testcard$);
end;
One more question then. If I do a for loop:
Global(CC$);
for(i$,0,12)
NewObject(CC$, "MyClass", MyObject);
...blah blah do some processing of the object.
end;

the object CC$ creates a new object 13 times. If I freeobject(CC$) after the for loop will I have 12 more objects somewhere in memory or does each time I recreate the CC$ object it erases the old CC$ object?
You will only free the 13th object from memory, the 12 others will stay in memory. You need to store the object's addresses somewhere to free them later.