Changing an NSImageView's NSImage
Changing an NSImageView's NSImage
- Subject: Changing an NSImageView's NSImage
- From: "David Piasecki" <email@hidden>
- Date: Fri, 4 Jun 2004 11:12:51 -0700
I am trying to change an image in an NSImageView object, and I'm not
sure if I'm doing it correctly because my program tends to crash
sometimes when the drawRect method is called in my NSView (on which I'm
displaying all these NSImageView objects). Sometimes I get an error
message regarding freeing a malloc-created object after it had already
been freed, and sometimes the program crashes with no messages. Leaving
the program running with no calls to drawRect produces no errors.
The other thing I do that might be causing problems is that I change a
flag in the drawRect method that is also being edited elsewhere by a
pthread. To separate the two, I surrounded the changing of the flag
with calls to pthread_mutex_lock(&lock) and
pthread_mutex_unlock(&lock). This should work, however I'm not sure
about the compatibility of Objective-C code and C++ with regards to
pthreads and mutex locks. I had put it in there because I read that
NSThreads were simply wrappers around pthreads, but perhaps there are
problems with that.
My gut tells me I'm not resetting the image properly since I'm new to
Obj-C. Maybe I should be releasing the old image before setting the new
one? I'm not sure if it matters, but the image I change to on condition
is an animated gif. I noticed that drawRect for the NSView gets called
every time the NSImageView's animated gif animates. I'm not sure if
this could be contributing to the problem or if it simply made it stand
out more since it was calling drawRect every 500ms.
-(void)drawRect:(NSRect *)rect
{
NSImageView *my_image;
for( int i = 0; i < num_images; i++ )
{
if( some_condition_changed() == true )
{
my_image = [my_image_array objectAtIndex:i];
[my_image setImage:[[NSImage alloc] initWith
Data:[[NSData alloc]
initWithBytes:my_new_data length:my_new_data_length]]];
setConditionChanged(false);
}
}
}
/* In one of my standard C++ files */
void Object::setConditionChanged( bool value )
{
pthread_mutex_lock(&lock);
conditionChanged = value;
pthread_mutex_unlock(&lock);
}
I realize I could be doing this all in Obj-C, but I'm attempting to
make my code as portable as possible while retaining the Cocoa look and
feel on the Mac. Any help or advice regarding changing the image or
dealing with threads would be greatly appreciated.
Thanks,
David
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.