English 中文(简体)
强迫保留新类别,从而放弃记忆
原标题:Forced to retain a new instance of a class and therefore leaking memory

我制定了一份财产清单,在无政府制时,首先出现了一系列称为可选择目标的群体。 可选择 物体类别并不包含一种内分法,它只是一些变数的集装箱,当然符合NSCoding。

可选择 选择了目标,根据选定目标,我就选择了一种可选择用途的子类。 引领这一动物。

我撰写了《动物目标》的习惯初衷,认为这是一种可选择的物体,是为了获得可选择的生物量值,然后又增加了自己的价值。 因此,这种方法认为:

- (AnimalObject *)initWithObject:(SelectableObject *)selectedObject

我从甄选屏幕上把它称作:

AnimalObject *animal = [[AnimalObject alloc] initWithObject:self.selectedObject];

如果是自发的。 选定 目标是从甄选屏幕中挑选的。

现在,在动物园地的初创者中,我确定了最初的数值,然后回到原地,叫 call。

[self saveToFile];

to save the new object to file. So having initialised it in the selection screen, I should be able to release it straight away right?

然而,如果我试图予以释放,即

    AnimalObject *animal = [[AnimalObject alloc] initWithObject:self.selectedObject];
[animal release];

我坠毁。

如果我不放电,我就得到警告,动物目标初始种植者中所有首选的单体动物都在泄露记忆。

我在选择Screen时试图创造动物遗产,在释放新物体之前将新物体转让给它,但这仍然坠毁,即。

self.newAnimal = animal;
[animal release];

我很想知道,从我上述描述来看,我是否做过错? 如同在初学者中拯救一班。 或者通过母子类作为反对子类初生者...... 我不清楚我为什么可以释放我所制造的物体。

感谢任何帮助!

EDIT Ok, here s my code:

选择目标类别:

#define kObjectNumberKey            @"ObjectNumber"
#define kObjectTypeKey              @"Type"
#define kObjectNameKey              @"Name"
#define kObjectThumbKey             @"Thumb"
#define kObjectMainKey              @"Main"

#import <Foundation/Foundation.h>

@interface SelectableObject : NSObject <NSCoding> {
    int         number;
    NSString    *type;
    NSString    *name;
    NSString    *thumbString;
    NSString    *mainString;
}

@property (nonatomic, assign) int number;
@property (nonatomic, retain) NSString *type;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *thumbString;
@property (nonatomic, retain) NSString *mainString;

@end

#import "SelectableObject.h"

@implementation SelectableObject

@synthesize number;
@synthesize type;
@synthesize name;
@synthesize thumbString;
@synthesize mainString;


- (void)dealloc {
    NSLog(@"Selectable Object DEALLOC");
    [name release];
    [type release];
    [thumbString release];
    [mainString release];
    [super dealloc];
}

#pragma mark -
#pragma mark NSCoding

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeInt:self.number forKey:kObjectNumberKey];
    [aCoder encodeObject:self.type forKey:kObjectTypeKey];
    [aCoder encodeObject:self.name forKey:kObjectNameKey];
    [aCoder encodeObject:self.thumbString forKey:kObjectThumbKey];
    [aCoder encodeObject:self.mainString forKey:kObjectMainKey];
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super init]) {
        self.number = [aDecoder decodeIntForKey:kObjectNumberKey];
        self.type = [aDecoder decodeObjectForKey:kObjectTypeKey];
        self.name = [aDecoder decodeObjectForKey:kObjectNameKey];
        self.thumbString = [aDecoder decodeObjectForKey:kObjectThumbKey];
        self.mainString = [aDecoder decodeObjectForKey:kObjectMainKey];
    }
    return self;
}

@end

另一类动物区系为新动物区,其细节如下:

- (void)createNewAnimal {
    self.selectedObject.name = self.nameField.text;

    Animal *animal = [[Animal alloc] initWithObject:self.selectedObject];
//  [animal release]; (causes it to crash)

    // Initialise the new Root Controller and load with selected object
}

动物类:

- (Animal *)initWithObject:(SelectableObject *)selectedObject {
    if (self = [super init]) {
        //Initialise all the values
        dateCreated = [[NSDate alloc] init];
        dateLastUsed = [[NSDate alloc] init];
        name = selectedObject.name;
        thumbString = selectedObject.thumbString;
        mainString = selectedObject.mainString;
    }   
    [self saveToFile];
    return self;
}
最佳回答

你的内心怀也许是一个怀疑者。 动物粪便释放 姓名,thumbStringmainString? 如果是的话,他们就会两次获释。

- (Animal *)initWithObject:(SelectableObject *)selectedObject {
    if (self = [super init]) {
        //Initialise all the values
        dateCreated = [[NSDate alloc] init];
        dateLastUsed = [[NSDate alloc] init];
        name = [selectedObject.name retain]; // Added retain
        thumbString = [selectedObject.thumbString retain]; // Added retain
        mainString = [selectedObject.mainString retain]; // Added retain
    }   
    [self saveToFile];
    return self;
}
问题回答

How does the dealloc() method of Animal look? Are you releasing name, thumbString and mainString in there? If yes, then there is your crash.

由于西区的枪声最快,答案已经由@imaginaryboy提供。 由于主食和umb子在超级班子中被释放,它将坠毁。 仅增加一纸空文法中的保留,而且你应当全部确定。

<Note:NSStrings>s 应当使用复制件而不是保留。

This is a complete shot in the dark but did you try

  AnimalObject *animal = [[[AnimalObject alloc] initWithObject:self.selectedObject] autorelease];

我不知道这里是否是问题,但直到现在,我才看到一些草率的方法,比如说,把巫术带回来。

- (id)init

我现在不想再回过一次。





相关问题
Subclass check, is operator or enum check

A couple of friends was discussing the use of inheritance and how to check if a subclass is of a specific type and we decided to post it here on Stack. The debate was about if you should implement a ...

C++ Class Inheritance problem

Hi I have two classes, one called Instruction, one called LDI which inherits from instruction class. class Instruction{ protected: string name; int value; public: Instruction(string ...

Overloading a method in a subclass in C++

Suppose I have some code like this: class Base { public: virtual int Foo(int) = 0; }; class Derived : public Base { public: int Foo(int); virtual double Foo(double) = 0; }; ...

Embedding instead of inheritance in Go

What is your opinion of this design decision? What advantages does it have and what disadvantages? Links: Embedding description

Extending Flex FileReference class to contain another property

I want to extend the FileReference class of Flex to contain a custom property. I want to do this because AS3 doesn t let me pass arguments to functions through event listeners, which makes me feel sad,...

Interface Inheritance in C++

I have the following class structure: class InterfaceA { virtual void methodA =0; } class ClassA : public InterfaceA { void methodA(); } class InterfaceB : public InterfaceA { virtual ...

热门标签