Check the result of the super class initializer and assign it to self.

Also fix two memory management errors.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/steptalk/trunk@36403 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Wolfgang Lux 2013-03-23 18:09:25 +00:00
parent 484ff95559
commit a31258c1d1
17 changed files with 240 additions and 172 deletions

View file

@ -1,3 +1,42 @@
2013-03-23 Wolfgang Lux <wolfgang.lux@gmail.com>
* STBlock.m (-initWithInterpreter:...):
* STBlockContext.m (initWithInterpreter:initialIP:stackSize:):
* STBytecodeInterpreter.m (-initWithEnvironment:):
* STBytecodes.m (-initWithData:, -initWithCoder:):
* STCompiledMethod.m (-initWihtSelector:argumentCount:bytecodeData:...)
-initWithCoder:):
* STCompiledScript.m (-initWithVariableNames:):
* STCompiler.m (-initWithEnvironment:, -init):
* STCompilerUtils.m(STCMethod) (-initWithPattern:statements:):
* STCompilerUtils.m(STCMessage) (-init):
* STCompilerUtils.m(STCMessageExpression) (-initWithTarget:message:):
* STCompilerUtils.m(STCPrimaryExpression) (-initWithObject:):
* STCompilerUtils.m(STCPrimary) (-initWithType:object:):
* STExecutionContext.m (-initWithStackSize:):
* STLiterals.m(STObjectReferenceLiteral) (initWithObjectName:poolName:):
* STLiterals.m(STBlockLiteral) (-initWithArgumentCount:):
* STMessage.m (-initWithSelector:arguments:):
* STMethodContext.m (-initWithMethod:environment:):
* STSmalltalkScriptObject.m (-initWithEnvironment:compiledScript:):
* STSourceReader.m (initWithString:range:):
* STStack.m (-initWithSize:):
Check the result of the super class initializer and assign it to
self.
* STBlock.m (-initWithInterpreter:..., -dealloc): Retain and release
the homeContext and interpreter attributes.
* STBytecodeInterpreter.m (-delloc): Release environment attribute to
fix space leak.
* STBytecodeInterpreter.m (createBlockWithArgumentCount:stackSize:):
* STCompiledMethod.m (+methodWithCode:messagePattern:):
* STCompilerUtils.m (+primaryWithVariable:, +primaryWithLiteral:,
+primaryWithBlock:, +primaryWithExpression:):
Use idiomatic Objective-C code, which never assigns the result of an
+alloc method to a local variable.
2013-03-02 Wolfgang Lux <wolfgang.lux@gmail.com>
* STSourceReader.m (_STNormalizeStringToken): Unescape two successive

View file

@ -48,15 +48,23 @@ Class STBlockContextClass = nil;
argumentCount:(int)count
stackSize:(int)size
{
homeContext = context;
argCount = count;
stackSize = size;
initialIP = ptr;
interpreter = anInterpreter;
return [super init];
if ((self = [super init]) != nil)
{
homeContext = RETAIN(context);
argCount = count;
stackSize = size;
initialIP = ptr;
interpreter = RETAIN(anInterpreter);
}
return self;
}
- (void)dealloc
{
RELEASE(homeContext);
RELEASE(interpreter);
[super dealloc];
}
- (unsigned)argumentCount
{
return argCount;

View file

@ -39,13 +39,13 @@
initialIP:(unsigned)pointer
stackSize:(unsigned)size
{
[super initWithStackSize:size];
interpreter = RETAIN(anInterpreter);
initialIP = pointer;
if ((self = [super initWithStackSize:size]) != nil)
{
interpreter = RETAIN(anInterpreter);
initialIP = pointer;
instructionPointer = initialIP;
instructionPointer = initialIP;
}
return self;
}
- (void)dealloc

View file

@ -93,10 +93,15 @@ static Class NSInvocation_class = nil;
- initWithEnvironment:(STEnvironment *)env
{
self = [super init];
environment = RETAIN(env);
if ((self = [super init]) != nil)
environment = RETAIN(env);
return self;
}
- (void)delloc
{
RELEASE(environment);
[super dealloc];
}
- (void)setEnvironment:(STEnvironment *)env
{
ASSIGN(environment,env);
@ -300,13 +305,12 @@ static Class NSInvocation_class = nil;
stackSize,
ptr);
block = [STBlock alloc];
[block initWithInterpreter:self
homeContext:[activeContext homeContext]
initialIP:ptr
argumentCount:argCount
stackSize:stackSize];
block =
[[STBlock alloc] initWithInterpreter:self
homeContext:[activeContext homeContext]
initialIP:ptr
argumentCount:argCount
stackSize:stackSize];
[stack push:AUTORELEASE(block)];
}

View file

@ -170,9 +170,12 @@ NSString *STDissasembleBytecode(STBytecode bytecode)
length: (unsigned)length
fromZone: (NSZone*)zone
{
bytes = [[NSData alloc] initWithBytesNoCopy:someBytes
length:length
fromZone:zone];
if ((self = [super init]) != nil)
{
bytes = [[NSData alloc] initWithBytesNoCopy:someBytes
length:length
fromZone:zone];
}
return self;
}
*/
@ -181,10 +184,10 @@ NSString *STDissasembleBytecode(STBytecode bytecode)
- (id) initWithData: (NSData *)data
{
self = [super init];
bytes = RETAIN(data);
if ((self = [super init]) != nil)
{
bytes = RETAIN(data);
}
return self;
}
@ -294,10 +297,10 @@ NSString *STDissasembleBytecode(STBytecode bytecode)
- initWithCoder:(NSCoder *)decoder
{
self = [super init]; // super initWithCoder: decoder];
[decoder decodeValueOfObjCType: @encode(id) at: &bytes];
if ((self = [super init] /*[super initWithCoder: decoder]*/) != nil)
{
[decoder decodeValueOfObjCType: @encode(id) at: &bytes];
}
return self;
}

View file

@ -38,13 +38,14 @@
stackSize:(unsigned)size
namedReferences:(NSMutableArray *)refs
{
[super init];
bytecodes = [[STBytecodes alloc] initWithData:data];
literals = [[NSArray alloc] initWithArray:anArray];
tempCount = count;
stackSize = size;
namedRefs = [[NSArray alloc] initWithArray:refs];
if ((self = [super init]) != nil)
{
bytecodes = [[STBytecodes alloc] initWithData:data];
literals = [[NSArray alloc] initWithArray:anArray];
tempCount = count;
stackSize = size;
namedRefs = [[NSArray alloc] initWithArray:refs];
}
return self;
}
- (void)dealloc
@ -94,14 +95,14 @@
- initWithCoder:(NSCoder *)decoder
{
self = [super init]; // [super initWithCoder: decoder];
[decoder decodeValueOfObjCType: @encode(id) at: &bytecodes];
[decoder decodeValueOfObjCType: @encode(id) at: &literals];
[decoder decodeValueOfObjCType: @encode(id) at: &namedRefs];
[decoder decodeValueOfObjCType: @encode(short) at: &tempCount];
[decoder decodeValueOfObjCType: @encode(short) at: &stackSize];
if ((self = [super init] /*[super initWithCoder: decoder]*/) != nil)
{
[decoder decodeValueOfObjCType: @encode(id) at: &bytecodes];
[decoder decodeValueOfObjCType: @encode(id) at: &literals];
[decoder decodeValueOfObjCType: @encode(id) at: &namedRefs];
[decoder decodeValueOfObjCType: @encode(short) at: &tempCount];
[decoder decodeValueOfObjCType: @encode(short) at: &stackSize];
}
return self;
}

View file

@ -41,15 +41,14 @@
{
STCompiledMethod *method;
method = [STCompiledMethod alloc];
[method initWithSelector:[pattern selector]
argumentCount:[[pattern arguments] count]
bytecodesData:[[code bytecodes] data]
literals:[code literals]
temporariesCount:[code temporariesCount]
stackSize:[code stackSize]
namedReferences:[code namedReferences]];
method =
[[STCompiledMethod alloc] initWithSelector:[pattern selector]
argumentCount:[[pattern arguments] count]
bytecodesData:[[code bytecodes] data]
literals:[code literals]
temporariesCount:[code temporariesCount]
stackSize:[code stackSize]
namedReferences:[code namedReferences]];
return AUTORELEASE(method);
@ -63,15 +62,15 @@
stackSize:(unsigned)size
namedReferences:(NSMutableArray *)refs;
{
self = [super initWithBytecodesData:data
literals:anArray
temporariesCount:tCount
stackSize:size
namedReferences:refs];
selector = RETAIN(sel);
argCount = aCount;
if ((self = [super initWithBytecodesData:data
literals:anArray
temporariesCount:tCount
stackSize:size
namedReferences:refs]) != nil)
{
selector = RETAIN(sel);
argCount = aCount;
}
return self;
}
- (void)dealloc
@ -132,11 +131,11 @@
- initWithCoder:(NSCoder *)decoder
{
self = [super initWithCoder: decoder];
[decoder decodeValueOfObjCType: @encode(id) at: &selector];
[decoder decodeValueOfObjCType: @encode(short) at: &argCount];
if ((self = [super initWithCoder: decoder]) != nil)
{
[decoder decodeValueOfObjCType: @encode(id) at: &selector];
[decoder decodeValueOfObjCType: @encode(short) at: &argCount];
}
return self;
}
- (NSString *)source

View file

@ -51,10 +51,10 @@ static SEL finalizeSelector;
- initWithVariableNames:(NSArray *)array;
{
[super init];
variableNames = RETAIN(array);
if ((self = [super init]) != nil)
{
variableNames = RETAIN(array);
}
return self;
}
- (void)dealloc

View file

@ -125,27 +125,27 @@ extern int STCparse(void *context);
}
- initWithEnvironment:(STEnvironment *)env
{
self = [self init];
[self setEnvironment:env];
if ((self = [self init]) != nil)
[self setEnvironment:env];
return self;
}
- init
{
[super init];
arrayLiteralClass = [NSMutableArray class];
stringLiteralClass = [NSMutableString class];
/* bytesLiteralClass = [NSMutableData class]; */
intNumberLiteralClass = [NSNumber class];
realNumberLiteralClass = [NSNumber class];
symbolLiteralClass = [STSelector class];
characterLiteralClass = [NSString class];
receiverVars = [[NSMutableArray alloc] init];
namedReferences = [[NSMutableArray alloc] init];
if ((self = [super init]) != nil)
{
arrayLiteralClass = [NSMutableArray class];
stringLiteralClass = [NSMutableString class];
/* bytesLiteralClass = [NSMutableData class]; */
intNumberLiteralClass = [NSNumber class];
realNumberLiteralClass = [NSNumber class];
symbolLiteralClass = [STSelector class];
characterLiteralClass = [NSString class];
receiverVars = [[NSMutableArray alloc] init];
namedReferences = [[NSMutableArray alloc] init];
}
return self;
}
- (void)dealloc

View file

@ -42,9 +42,11 @@
}
- initWithPattern:(STCMessage *)patt statements:(STCStatements *)stats
{
[super init];
messagePattern = RETAIN(patt);
statements = RETAIN(stats);
if ((self = [super init]) != nil)
{
messagePattern = RETAIN(patt);
statements = RETAIN(stats);
}
return self;
}
- (void)dealloc
@ -120,11 +122,11 @@
}
- init
{
[super init];
selector = [[NSMutableString alloc] init];
args = [[NSMutableArray alloc] init];
if ((self = [super init]) != nil)
{
selector = [[NSMutableString alloc] init];
args = [[NSMutableArray alloc] init];
}
return self;
}
- (void)dealloc
@ -218,11 +220,11 @@
@implementation STCMessageExpression:STCExpression
- initWithTarget:(id)anObject message:(STCMessage *)aMessage;
{
[super init];
target = RETAIN(anObject);
message = RETAIN(aMessage);
if ((self = [super init]) != nil)
{
target = RETAIN(anObject);
message = RETAIN(aMessage);
}
return self;
}
- (void)dealloc
@ -253,8 +255,8 @@
}
- initWithObject:(id)anObject
{
[super init];
object = RETAIN(anObject);
if ((self = [super init]) != nil)
object = RETAIN(anObject);
return self;
}
@ -276,36 +278,39 @@
+ primaryWithVariable:(id) anObject
{
STCPrimary *primary;
primary = [STCPrimary alloc];
[primary initWithType:STCVariablePrimaryType object:anObject];
primary = [[STCPrimary alloc] initWithType:STCVariablePrimaryType
object:anObject];
return AUTORELEASE(primary);
}
+ primaryWithLiteral:(id) anObject
{
STCPrimary *primary;
primary = [STCPrimary alloc];
[primary initWithType:STCLiteralPrimaryType object:anObject];
primary = [[STCPrimary alloc] initWithType:STCLiteralPrimaryType
object:anObject];
return AUTORELEASE(primary);
}
+ primaryWithBlock:(id) anObject
{
STCPrimary *primary;
primary = [STCPrimary alloc];
[primary initWithType:STCBlockPrimaryType object:anObject];
primary = [[STCPrimary alloc] initWithType:STCBlockPrimaryType
object:anObject];
return AUTORELEASE(primary);
}
+ primaryWithExpression:(id) anObject
{
STCPrimary *primary;
primary = [STCPrimary alloc];
[primary initWithType:STCExpressionPrimaryType object:anObject];
primary = [[STCPrimary alloc] initWithType:STCExpressionPrimaryType
object:anObject];
return AUTORELEASE(primary);
}
- initWithType:(int)newType object:obj
{
type = newType;
object = RETAIN(obj);
return [super init];
if ((self = [super init]) != nil)
{
type = newType;
object = RETAIN(obj);
}
return self;
}
- (void)dealloc
{

View file

@ -25,11 +25,11 @@ static unsigned nextId = 1;
@implementation STExecutionContext
- initWithStackSize:(unsigned)stackSize
{
[super init];
stack = [[STStack alloc] initWithSize:stackSize];
contextId = nextId ++;
if ((self = [super init]) != nil)
{
stack = [[STStack alloc] initWithSize:stackSize];
contextId = nextId ++;
}
return self;
}
- (void)dealloc

View file

@ -32,9 +32,12 @@
@implementation STObjectReferenceLiteral
- initWithObjectName:(NSString *)anObject poolName:(NSString *)aPool
{
objectName = RETAIN(anObject);
poolName = RETAIN(aPool);
return [super init];
if ((self = [super init]) != nil)
{
objectName = RETAIN(anObject);
poolName = RETAIN(aPool);
}
return self;
}
#if 0
- copyWithZone:(NSZone *)zone
@ -68,8 +71,9 @@
@implementation STBlockLiteral
- initWithArgumentCount:(unsigned)count
{
argCount = count;
return [super init];
if ((self = [super init]) != nil)
argCount = count;
return self;
}
- (void)setStackSize:(unsigned)size
{

View file

@ -44,10 +44,11 @@
- initWithSelector:(NSString *)aString
arguments:(NSArray *)anArray
{
[super init];
selector = RETAIN(aString);
args = RETAIN(anArray);
if ((self = [super init]) != nil)
{
selector = RETAIN(aString);
args = RETAIN(anArray);
}
return self;
}

View file

@ -51,20 +51,22 @@
- initWithMethod:(STCompiledMethod *)newMethod
environment:(STEnvironment *)env
{
unsigned int tempCount;
unsigned int i;
method = RETAIN(newMethod);
tempCount = [method temporariesCount];
temporaries = [[NSMutableArray alloc] initWithCapacity:tempCount];
for(i=0;i<tempCount;i++)
if ((self = [super initWithStackSize:[method stackSize]]) != nil)
{
[temporaries insertObject:STNil atIndex:i];
unsigned int tempCount;
unsigned int i;
method = RETAIN(newMethod);
tempCount = [method temporariesCount];
temporaries = [[NSMutableArray alloc] initWithCapacity:tempCount];
for (i=0; i<tempCount; i++)
{
[temporaries insertObject:STNil atIndex:i];
}
}
return [super initWithStackSize:[method stackSize]];
return self;
}
- (void)dealloc

View file

@ -44,26 +44,26 @@
- initWithEnvironment:(STEnvironment *)env
compiledScript:(STCompiledScript *)compiledScript
{
NSEnumerator *enumerator;
NSString *varName;
[super init];
NSDebugLLog(@"STEngine",
@"creating script object %p with ivars %@",compiledScript,
[compiledScript variableNames]);
environment = RETAIN(env);
script = RETAIN(compiledScript);
variables = [[NSMutableDictionary alloc] init];
enumerator = [[compiledScript variableNames] objectEnumerator];
while( (varName = [enumerator nextObject]) )
if ((self = [super init]) != nil)
{
[variables setObject:STNil forKey:varName];
NSEnumerator *enumerator;
NSString *varName;
NSDebugLLog(@"STEngine",
@"creating script object %p with ivars %@",compiledScript,
[compiledScript variableNames]);
environment = RETAIN(env);
script = RETAIN(compiledScript);
variables = [[NSMutableDictionary alloc] init];
enumerator = [[compiledScript variableNames] objectEnumerator];
while ((varName = [enumerator nextObject]) != nil)
{
[variables setObject:STNil forKey:varName];
}
}
return self;
}
- (void)dealloc

View file

@ -154,14 +154,14 @@ static NSString *_STNormalizeStringToken(NSString *token)
- initWithString:(NSString *)aString range:(NSRange)range
{
[super init];
source = RETAIN(aString);
srcRange = range;
srcOffset = range.location;
tokenRange = NSMakeRange(0,0);
tokenType = STInvalidTokenType;
if ((self = [super init]) != nil)
{
source = RETAIN(aString);
srcRange = range;
srcOffset = range.location;
tokenRange = NSMakeRange(0,0);
tokenType = STInvalidTokenType;
}
return self;
}

View file

@ -23,11 +23,13 @@
- initWithSize:(unsigned)newSize
{
size = newSize;
pointer = 0;
stack = NSZoneMalloc( NSDefaultMallocZone(), size * sizeof(id) );
return [super init];
if ((self = [super init]) != nil)
{
size = newSize;
pointer = 0;
stack = NSZoneMalloc( NSDefaultMallocZone(), size * sizeof(id) );
}
return self;
}
- (void)invalidPointer:(unsigned)ptr