instance management in IB
instance management in IB
- Subject: instance management in IB
- From: Chris Carson <email@hidden>
- Date: Mon, 11 May 2009 18:29:25 -0700 (PDT)
Hello,
I've been scratching my head trying to get a basic delegate/data source Cocoa/AppKit program working and feel that I'm misunderstanding something basic (I've included the code is at the end).
I set a breakpoint within the if statement in the init method of MyModel and see it called more than once. The second time the debugger throws a EXC_BAD_ACCESS. This message goes away if I comment out the two lines in stepAnimation that call the getBuffer and getLength methods and then modify the buffer. All this makes me think that somehow the instance of MyModel is being deallocated as soon as the data source message returns, but before the data is copied into the pointBuffer.
I created an NSView and NSObject in Interface Builder, assigning the MyView subclass to the NSView object and MyModel to the NSObject object. Then I connected the dataSource outlet on MyView to th MyModel object.
What am I missing? How can I get the data from MyModel to be viewable my MyView?
Sidenote: Ultimately I'm trying to plot a real-time frequency spectrum. My plan is to pull in data over a CFMessagePort in the model and have the view access it view the data source.
Help is much appreciated!
Chris
main.m
----------
int main(int argc, char *argv[]) {
// Launch Cocoa application
return NSApplicationMain(argc, (const char **) argv);
}
MyView.m:
----------
@implementation MyView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
pointPath = [NSBezierPathbezierPath];
pointBuffer = malloc (sizeof(NSPoint) * 100);
[NSTimerscheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(stepAnimation:)
userInfo:nil
repeats:YES];
}
return self;
}
- (void)drawRect:(NSRect)rect {
[pointPath removeAllPoints];
[pointPathappendBezierPathWithPoints:(NSPointArray)pointBuffercount:100];
[pointPathstroke];
}
- (void)setDataSource:(id)inDataSource {
dataSource = inDataSource;
}
- (id)dataSource {
returndataSource;
}
- (void)stepAnimation:(NSTimer *)timer {
float*inBuffer;
int i, length;
// Fetch buffer pointer
inBuffer = [[self dataSource] getBuffer:self];
length = [[self dataSource] getLength:self];
// Copy to point buffer
for (i = 0; i < length; i++) {
pointBuffer[i].y = inBuffer[i];
pointBuffer[i].x = i;
}
// Mark display for painting
[selfsetNeedsDisplay:YES];
}
- (void)dealloc {
free(pointBuffer);
[super dealloc];
}
@end
MyView.h
----------
@interface MyView : NSView {
IBOutletid dataSource;
NSBezierPath *pointPath;
NSPoint *pointBuffer;
}
- (void)setDataSource:(id)inDataSource;
- (id)dataSource;
- (void)stepAnimation:(NSTimer *)timer;
@end
@interface NSObject(MyViewDataSource)
- (float *)getBuffer:(MyView *)view;
- (int)getLength:(MyView *)view;
@end
MyModel.m
----------
@implementationMyModel
- (id)init {
self= [superinit];
if (self) {
buffer = malloc (sizeof(float) * 100);
length = 100;
}
return self;
}
- (float *)getBuffer:(GraphView *)view {
returnbuffer;
}
- (int)getLength:(GraphView *)view {
returnlength;
}
- (void)dealloc {
free (buffer);
[super dealloc];
}
@end
MyModel.h
----------
@interface MyModel : NSObject {
float *buffer;
int length;
}
- (float *)getBuffer:(GraphView *)view;
- (int)getLength:(GraphView *)view;
@end
_______________________________________________
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