Some ObjC help needed

Could someone have a look at the attached files - it is a sub-class of NSTextField that is supposed to scroll its content if it is too long and does not fit the box.

There seems to be some memory management-related problem in the implementation, causing application crash from time to time (mostly when changing the contents of the field, so I guess the problem is in -setStringValue: method). I am pretty sure there is some small and stupid error in the code, but I just don't see it :-(

There seems to be some memory management-related problem in the implementation, causing application crash from time to time (mostly when changing the contents of the field, so I guess the problem is in -setStringValue: method). I am pretty sure there is some small and stupid error in the code, but I just don't see it :-(

I haven't extended a UI object, so take this all with a grain of salt.

The first thing I see if that you are using the -display from NSView method rather than -setNeedsDisplay from NSControl. The first forces the display, while the second just marks it as needing the display and lets the run loop handle updating it when it gets around it to. All the examples I have seen use the later approach.

The second thing is that (at a quick glance) I wonder if you really need a sub-class of the object. Why not just make a delegate object that listens for the NSControlTextDidChangeNotification notification and then update your position accordingly?

The only thing that jumps out at me is how you handle attrString. You are not initializing it anywhere, so your -autorelease call on it (should be -release rather than -autorelease anyway) in -setStringValue is going to have unknown results. Secondly when you are creating the string later, you are not issuing a -retain call on it which means it goes out of scope (and see the previous issue).

I would suggest that you:
1) Add an -init method to (at a minimum) set all your class variable to nil (or 0).

2) Test for attrString to not be nil before you try to release it.

3) Add a retain call when you create attrString so that it is available to all your methods and is carried over from call to call (if that is indeed it's purpose as it appears).

Finally, I have spent many hours debugging what I thought was the problem (based the debugger) only to find that it was actually an entirely different object causing the issue. If none of the above helps, i'd try throwing NSLog's every where to figure out exactly where it is crashing (my case was a completly unrelated object being nil when I tried to reference it, but the debugger had me looking at something else ).

Ups, I uploaded the old version of the files - this version worked fine, except 60% of the processor load (apparently due to text rendering :-O ).

Here is the newest version - it creates an NSImage representation of the string during -setStringValue: and then only draws that image. This reduces the processor load to a fraction of what it used to be.

Thanks for your notes. Will look at this today and see whether it helps.

Here is the newest version - it creates an NSImage representation of the string during -setStringValue: and then only draws that image. This reduces the processor load to a fraction of what it used to be.

Jirka,
Sorry it took awhile. In this version, I don't see anything that jumps out at me beyond the items mentioned above (though textImage has the same init, retain, and release issues as attrString).
Hope some of the above helps.