I have an object system which I wrote in C which contains reference counting for objects (objects are just structs which have a retainCount int). If I have a block like the following:
typedef void (^MyBlock)();
void doBlockStuff(MyBlock b){
Block_copy(b);
//...
}
__block int i=0;
doBlockStuff(^{
++i;
});
then the runtime heap-allocates the integer i when Block_copy is called. However, if I use a reference-counted object instead:
typedef void (^MyBlock)();
void doBlockStuff(MyBlock b){
Block_copy(b);
//...
}
__block Object* obj=Object_New();
doBlockStuff(^{
DoObjectStuff(obj);
});
then the pointer itself, not it’s referenced value, is heap-allocated by the runtime (although it is already heap-allocated by the Object_New function). Because the object is reference counted, another function could come along and release the object before the block is released. If I explicitly retain the object, then it will never be freed. So, my question is, how do I add a callback to Block_dealloc to explicitly release the object when it is freed?
Thanks.
Wrap your C Object* in a __block storage C++ type. Something like so:
Class:
Usage: