There is a good discussion about the safety of using malloc/free in objective-c here As long as you correctly free() the memory that you malloc(), there should be no issue I personally think that using NSMutableData or NSMutableArray is just easier. If you don't need ultimate performance, I would not use the C malloc/free statements directly.
There is a good discussion about the safety of using malloc/free in objective-c here. As long as you correctly free() the memory that you malloc(), there should be no issue. I personally think that using NSMutableData or NSMutableArray is just easier.
If you don't need ultimate performance, I would not use the C malloc/free statements directly.
That helped lead me to the answer which was actually unrelated to my use of free. Using free(self. Property); was incorrect.Facepalm.
I tried free(property); which works out fine. In the future I believe I will use NSData to avoid the hassle of malloc. – XenElement Jul 30 at 19:16.
One way around this sort of issue is to use NSMutableData, so you can replace unsigned char *rawData = malloc(height * width * 4); with myData = NSMutableData alloc initWithCapacity:height * width * 4; unsigned char *rawData = myData. MutableBytes; you can then release myData in your deallocator. Alternativly you can do myData = NSMutableData dataWithCapacity:height * width * 4; This will then mean your myData is kept around the the duration of the event loop, you can of cause even change the return type of getRawData method to return NSMUtableData or NSData, and that way it can be retained by other parts of your code, the only time I return raw bytes in my code is if I know it will be available for the life of the object that returns it, that way if I need to hold onto the data I can retain the owner class.
Apple will often use the myData = NSMutableData alloc initWithCapacity:height * width * 4; unsigned char *rawData = myData. MutableBytes; pattern and then document that if you need the bytes beyond the current autorelease pool cycle you will then have to copy it.
The object must be released, the raw pointer must be freed. In both cases, the design should be thus that it is pretty clear when they can be discarded. – Rudy Velthuis Jul 30 at 19:32 It easier to deal with an Objects because you can then stick to the retain/release rules.
If you return raw bytes from a method, how do you known the caller of the method manages the memory correctly, what if the caller of the method is not interested in the returned bytes. If the bytes are returned from a private method then you can do what every you like, but if it is part of the public interface then making stuff idiot proof so you don't have to remember that method you wrote 6 months ago doesn't behave in the usually way, it much simpler to stick to a simple set of rules. – Nathan Day Jul 30 at 20:08 That is why I wouldn't return memory.
I would tell the caller how big it must be and then the caller can pass a buffer he or she allocated to you. You just fill the buffer. That way, ownership is much clearer, IMO.
– Rudy Velthuis Jul 30 at 20:35 Yes that is a good approach that I have often adopted a lot, but in some situation, returing the bytes of autoreleased of NSData is useful, I think some of the NSString methods do that like - NSString UTF8String – Nathan Day Jul 30 at 20:57.
There is a good discussion about the safety of using malloc/free in objective-c here .
One way around this sort of issue is to use NSMutableData, so you can replace.
And in another class I assign a property to that pointer like so: self. Where in this process can I free that malloc'd memory? When I try to free the property in dealloc, it gives me an exc_bad_access error.
I feel like I'm missing a fundamental c or objective-c concept here. All help is appreciated.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.