0

If I create an object in C++ code and return it in ActionScript code should I call AS3_Release before returning it? For example, I have the function in the *.gg file:

public function makeThumbnail(...): Object
{
     AS3_Val objDestByteArray = AS3_New(ByteArray_class, no_params);
     int intDestWidth;
     int intDestHeight;

     // ...  make some calculations and set results as object properties

     AS3_Val result = AS3_Object("width:IntType, height:IntType, data:AS3ValType", intDestWidth, intDestHeight, objDestByteArray);

     // Do I need to call this?
     //AS3_Release(objDestByteArray);
     //AS3_Release(result);

     return result;
}

Should I call AS3_Release for objDestByteArray and result variables?

Andrey M.
  • 3,688
  • 3
  • 33
  • 36

1 Answers1

1

All unique AS3_Val variables need to be released eventually. For AS3_Val return variables, the function doesn't release the value itself but instead assumes that its caller will arrange for the value's eventual release.

So in your example, release objDestByteArray but don't release result yet. Whoever is calling makeThumbnail is responsible for releasing its return value.

Gunslinger47
  • 7,001
  • 2
  • 21
  • 29
  • Thanks for the answer! Can you please explain a bit about the `result` variable? This object will be returned to the actionscript code, where we have not method to release objects, because GC handle all references automatically. So I just should hope that GC will clean this object eventually? – Andrey M. Feb 01 '11 at 20:28
  • @Andrey: Yes. If your function returns an `AS3_Val` with a reference count of 1, then the complete responsibility for cleaning this value is transferred to the caller. This includes callback functions where Alchemy calls your functions and assumes responsibilty for cleaning up the return values. – Gunslinger47 Feb 02 '11 at 03:55
  • I should add that releasing `result` then returning it with a reference count of 0 isn't even an option. The value may cease to exist immediately after you call `AS3_Release`, making it unavailable for return. – Gunslinger47 Feb 02 '11 at 03:58
  • I've asked the same question on the alchemy forum and got a little different answer: http://forums.adobe.com/message/3441737 They says I don't need to release `result` at all. When I return this object there is no references to it in C code. I also checked it in Flash Builder profiler and got the same result too. After returning from C code to actionscript the `objDestByteArray` still has references from C code and `result` doesn't. But anyway, your answer is correct. I should call `AS3_Release(objDestByteArray);`, but should not `AS3_Release(result);`. – Andrey M. Feb 02 '11 at 04:49
  • @Andrey: He was simplifying it for you a little by saying that return "releases" the reference. We're essentially saying the same thing. Don't completely release values prior to returning them. – Gunslinger47 Feb 02 '11 at 06:15