Re: subclass overwriting superclass ivar
Re: subclass overwriting superclass ivar
- Subject: Re: subclass overwriting superclass ivar
- From: Greg Parker <email@hidden>
- Date: Wed, 26 May 2010 12:00:57 -0700
On May 26, 2010, at 4:40 AM, email@hidden wrote:
A subclass ivar is apparently overwriting a super class ivar.
When an instance of MGS_B sets stderrData the super class ivar
tempFilePath gets overwritten.
Moving tempFilePath up the ivar list resolves the problem (though
doubtless creates another).
Moving ivar stderrData into the superclass also resolves it.
Does the problem lie in the interface declarations below or are
things likely going astray at run time?
Mac or iPhone?
iPhone device or iPhone simulator?
32-bit Mac or 64-bit Mac?
My guess is that (1) you're running on iPhone Simulator or 32-bit Mac,
and (2) you recently added an ivar to MGS_A but did not recompile all
code that uses MGS_B. In that case, any MGS_B code that was compiled
with the old declaration of MGS_A will think MGS_A is smaller than it
actually is, and write its ivar values to the wrong place.
Try a clean rebuild of everything. If the above is the problem, a
clean build should fix it.
You can also check ivar offsets that the runtime expects. These two
values should be different:
#include <objc/runtime.h>
printf("tempFilePath %zu, stderrData %zu\n",
(size_t)ivar_getOffset(class_getInstanceVariable
(objc_getClass("MGS_A"), "tempFilePath")),
(size_t)ivar_getOffset(class_getInstanceVariable
(objc_getClass("MGS_B"), "stderrData")));
iPhone device and 64-bit Mac should not have this problem. The
compiler and runtime cooperate to allow the runtime to move the
subclass's ivars out of the way if the superclass grows.
http://sealiesoftware.com/blog/archive/2009/01/27/objc_explain_Non-fragile_ivars.html
Note that gdb now includes improved support for watching ivars:
(gdb) watch -location self->ivar
@interface MGS_A : NSObject <NSApplicationDelegate> {
@private
int argc;
const char **argv;
NSDictionary *taskDict;
NSString *error;
NSInteger errorCode;
NSMutableDictionary *errorInfo;
NSMutableDictionary *replyDict;
int stdoutSaved;
id resultObject;
id scriptObject;
NSString *scriptExtension;
NSString *scriptSourceExtension;
MGSScriptExecutorManager *scriptExecutorManager;
NSString *tempFilePath;
}
@interface MGS_B : MGS_A {
@private
NSData *stderrData;
}
--
Greg Parker email@hidden Runtime Wrangler
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden