Strange behavior in IOS while integrating with a c/c++ library.
In AppDelegate i call
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0), ^{
[[ABCService sharedInstance] abcInitialize];
});
abcInitialize is defined in c++ library
struct abc *top;
top = calloc(TYPE_SERV,size_of(struct abc));
top->us = server_alloc (...certain_params...);
inside server alloc i do alloc of structures struct1 and struct2 And then in Initialize function i try to access
top->us = calloc(TYPE_US, size_of(struct us));
if(top->us->struct1) //do something
I noticed that my struct1 is always null
When i debugged, i can see that the structure was allocated fine and values are set appropriately, but before returning from the function, the memory is automatically deallocated. This is totally confusing and annoying. Could any one help me understand what is going on?
I tried by turning off ARC too, still no change
top->us = calloc(TYPE_US, size_of(struct us)); if(top->us->struct1) //do something
I noticed that my struct1 is always null
If that's your real code, then yes, struct1
will always be null. calloc
allocates memory and initializes it to zero. Hence, all of the members of top->us
will be zero, and since struct1
is a member of top->us
, it's going to be zero, i.e. NULL
.
First of all, ARC has nothing to do with memory management of C or C++ codes, so, you can rule out ARC for causing this.
According to http://www.cplusplus.com/reference/cstdlib/calloc/, calloc always initialize all the bits of the allocated memory to 0, naturally, top->us->struct1 will be NULL.
but before returning from the function, the memory is automatically deallocated
How do you come to the conclusion that the memory is automatically deallocated? As mentioned above, ARC will not automatically release your C++ structure/object.
As a conclusion. The result you saw was indeed as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With