开发者

autorelease makes my app crash

开发者 https://www.devze.com 2023-02-17 04:11 出处:网络
My app crashes sometimes at this point: NSMutableData* position = [NSMutableData dataWithLength: 3 * sizeof(CGPoint)];

My app crashes sometimes at this point:

NSMutableData* position = [NSMutableData dataWithLength: 3 * sizeof(CGPoint)];

when NSMutableData calls the autorelease.

Here's the crash log:

Exception Type:  EXC_BREAKPOINT (SIGTRAP)
Excepti开发者_如何学JAVAon Codes: 0x00000001, 0xe7ffdefe
Crashed Thread:  0

Thread 0 name:
Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   CoreFoundation            0x343dea60 _CFAutoreleasePoolAddObject + 136
1   CoreFoundation                0x343de9cc -[NSObject(NSObject) autorelease] + 8
2   Foundation                0x30c49166 +[NSMutableData(NSMutableData) dataWithLength:] + 34

Do you have any idea why?

UPDATE: The variable "position" is returned and used this way:

    NSMutableData *positionData = [[SFinder sharedFinder] move:self.crId] ;
CGPoint *path = [positionData mutableBytes];
CGPoint location0 = path[0];

CGPoint location1 = path[1];

CGPoint location = path[2];

UPDATE 2:

I removed the code with NSMutableData, but the app keeps crashing randomly. On the debug console there is a message:

*** attempt to pop an unknown autorelease pool (0x5830000)

stack:

Thread 0 Crashed:
   Dispatch queue: com.apple.main-thread
0   CoreFoundation               0x01348b69 _CFAutoreleasePoolPop + 201
1   UIKit                        0x008a947c _wrapRunLoopWithAutoreleasePoolHandler + 68
2   CoreFoundation               0x013edfbb __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
3   CoreFoundation               0x013830e7 __CFRunLoopDoObservers + 295
4   CoreFoundation               0x0134bbd7 __CFRunLoopRun + 1575
5   CoreFoundation               0x0134b240 CFRunLoopRunSpecific + 208
6   CoreFoundation               0x0134b161 CFRunLoopRunInMode + 97
7   GraphicsServices             0x029b3268 GSEventRunModal + 217
8   GraphicsServices             0x029b332d GSEventRun + 115
9   UIKit         0x008a942e UIApplicationMain + 1160
10  MyApp                        0x000029f4 main + 100 (main.m:13)
11  MyApp                        0x00002985 start + 53


Found the problem. There is an issue with NSMutableData. It occures when you init it with initWithLength, or dataWithLength and try to insert data trough mutableBytes. When you insert the last byte it makes some allocation somewhere else (even if that part of the memory is owned by an other object) causing EXC_BAD_ACCESS and other memory related errors. So initiating it with length+1 bytes solved the problem.


when NSMutableData calls the autorelease.

??

it calls autorelease by itself? meaning? do you use:

[position release];

or

[position autorelease];

if so... well, don't do it... you don't really need it, if you initiated it with just the code you wrote in your question...


Did you retain the variable "position" in any other code? Typically if you released it and then later on autoreleasepool is drained, thus releasing it again, when it doesn't exist anymore. Please see this post for possible details.

Also you shouldn't have your variable call autorelease on itself (if you are).


Either:

1) There's something up with the enclosing autorelease pool (where is it? Is it one you defined yourself, or is it the default one?)

or

2) Something is releasing your NSMutableData* position when it shouldn't be. It is doing it before the current NSAutoreleasePool leaves its scope, sending the retain count to 0 and causing dealloc to be called -- and then when the autorelease pool itself calls release, you're crashing out because the object has already been dealloc'd.


The object the "position" variable refers to is automatically placed into the default autorelease pool by virtue of the fact that it originates from an NSMutableData convenience method. It may be that the default autorelease pool is no longer valid. I'm just guessing here.

In any case, objects in the default autorelease pool are guaranteed to be retained only for as long as the life of the current event context. This might have something to do with what your are seeing.

I would suggest you try making the "position" variable an instance property with attribute "retain". This will give you more control on the lifetime of the "position" object. Don't forget to release the "position" object in your class's dealloc method.

In all of my iOS apps, I rely heavily on the declared properties feature provided by the objective C framework. If I desire an object to "stick around" for awhile, I refer to it with a declared property with attribute "retain". Then I have a much clearer idea as to exactly how long it will remain valid. I also have more control on it's lifetime.

Hope this helps.

  • Let It Be Known
0

精彩评论

暂无评论...
验证码 换一张
取 消