Find in this group all groups
Unknown more information…

o : 13 January 2006 • 7:58AM -0500

Re: Custom constant string class crashes
by Timothy J. Wood


On Jan 12, 2006, at 11:18 AM, Will Mason wrote:

> The following program under the Next Objective-C runtime crashes  
> when compiled like this:
> gcc -o ConstantCrash -fconstant-string-class=MyConstantString  
> ConstantCrash.m -lobjc
> Under the GNU runtime this program works just fine. It seems that  
> the compiler option -fconstant-string-class is basically broken  
> under the Next runtime.
> Am I doing something dumb, or is this a bug?

   Not dumb, you just need to read the compiler source to figure out  
how to get this to work :)

   Below is a version that works.  The key points you were missing  
are the type of _MyConstantStringClassReference and copying in the  
compiler generated class.  Don't get me wrong, this is a totally  
goofy way for this to work, but there you go.

   Maybe there is some issue with getting it to link, but in a sane  
world, I'd want @"" to emit a 3-word tuple with the first one being a  
relocation to the actual class structure generated by the compiler.  
Some of the compiler folks on the list can comment to this better  
than I can.

   BTW, the 'objc_addClass' call below isn't really necessary for  
this to work, but it makes sure that [@"foo" class] results in  
something that is registered as a class.  Also, the +load works, even  
though it looks like a method since it gets compiled into a function  
marked as a constructor (i.e., it isn't called by objc_msgSend, as  
far as I know).


#import <objc/Object.h>
#import <objc/objc-runtime.h>
#import <stdio.h>
#import <strings.h>

@interface MyConstantString : Object
     char*       c_string;
     unsigned    length;

static struct objc_class _MyConstantStringClassReference;

@implementation MyConstantString
+ (void)load;
     // our @"" strings will have their isa pointing at  
_MyConstantStringClassReference, but this is empty and isn't in the  
class hash.

     // Copy over the compiler-generated class.  Note that if there  
are any subclasses of MyConstantString, they'll point to the compiler-
generated class as their superclass!
     Class cls = objc_getClass("MyConstantString");
     memcpy(&_MyConstantStringClassReference, cls, sizeof

     // Override the compiler version of the class

- (const char *)name;
     return c_string;

int main()
     printf("I would be a %s if I didn't crash\n", [@"DoggyBoy" name]);
     return 0;

Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      (Objc-language@list...)
Help/Unsubscribe/Update your Subscription:

This email sent to subscriber@open...

Bookmark with:

Delicious   Digg   reddit   Facebook   StumbleUpon

Related Messages

opensubscriber is not affiliated with the authors of this message nor responsible for its content.