I am developing an iPhone application which has a module written in C and Objective C.
I am using pthread_create for creating new thread.
For memory allocation, i have used malloc().
-
Will NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]
handle this memory? Or should i explicitly use free() to release this memory?The thread is running continuously and it is allocating lot of memory. So after one point, it gives memory warning.
-
Will it create problem if i explicitly free the memory as well as i use NSAutoreleasePool?
-
Is there any way to check the memory leaks in this thread. Xcode’s profile tool is used to check memory leaks in objective C code. will it help to manage C type memory allocation?
Please help me out.
you should use
freefor all memory allocated throughmallocnone. you have to do that as well.
as far as I understand, Instruments Leaks is tightly related to the ObjC runtime system, so it does not work for memory directly (meaning, not via the ObjC runtime) allocated through
malloc. For MacOSX, Apple provides a couple of tools to detect memory leaks (MallocDebug and leaks, see here), but they do not work for iOS apps. You might try to run them against the simulator, but I suspect they would not give you reliable results.On the other hand, if you are using a plain-C library, then you might build around it a plain-C testbed derived from your overall code (by removing the iOS specific parts) to be able to check it under MacOSX. The testbed would emulate the kind of workflow of your app with the C-only module. This would be a significant effort, though, and I would go for it only if you are really worried about it.
Another approach you have is building and ObjC wrapper around your C module, so that you have one or more classes that use the C module and allocate/release memory for it through malloc/free in the init/dealloc methods; you could check that your ObjC wrapper objects are not leaked by using Instruments and if you have correctly implemented the wrapper objects you could be pretty sure that you are not leaking malloc memory.