[MacRuby-devel] Frameworks Help

Shaun August shaun at eoslightmedia.com
Sat Oct 30 20:48:16 PDT 2010


Hi Mario,

Thanks again for your advice! I have managed to stabilize the program and I didn't end up using the NSLock function. While I was reading up on Apple's Threading Programming Guide looking for information about NSLock I came across this gem:

Protecting the Cocoa Frameworks

For multithreaded applications, Cocoa frameworks use locks and other forms of internal synchronization to ensure they behave correctly. To prevent these locks from degrading performance in the single-threaded case, however, Cocoa does not create them until the application spawns its first new thread using the NSThread class. If you spawn threads using only POSIX thread routines, Cocoa does not receive the notifications it needs to know that your application is now multithreaded. When that happens, operations involving the Cocoa frameworks may destabilize or crash your application.
To let Cocoa know that you intend to use multiple threads, all you have to do is spawn a single thread using the NSThread class and let that thread immediately exit. Your thread entry point need not do anything. Just the act of spawning a thread using NSThread is enough to ensure that the locks needed by the Cocoa frameworks are put in place.
If you are not sure if Cocoa thinks your application is multithreaded or not, you can use theisMultiThreaded method of NSThread to check.

After following the advice of quickly spawning a thread on startup I managed to fix my crash. The C framework I am calling is using the posix threads and it looks like this fixed it.

Thanks,

Shaun




On 2010-10-29, at 11:46 PM, Mario Steele wrote:

> Hello Shaun,
> 
> On Sat, Oct 30, 2010 at 1:41 AM, Shaun August <shaun at eoslightmedia.com> wrote:
> Hi All,
> 
> I have had some limited success going back and forth between the c framework, objective-c wrapper and MacRuby. 
> 
> I am getting numerous malloc errors when I start up the application. The application sort of runs after the first malloc error but after the second error it crashes almost immediately. 
> 
> 
> 2010-10-29 21:31:06.323 YYY[3585:a0f] awaking the Phidgets
> YYY(3585,0x10374d000) malloc: *** auto malloc[3585]: error: GC operation on unregistered thread. Thread registered implicitly. Break on auto_zone_thread_registration_error() to debug.
> 
> 2010-10-29 21:31:06.469 YYY[3585:5003] phidget Added
> 2010-10-29 21:31:06.563 YYY[3585:a0f] Phidget Added!
> 2010-10-29 21:31:06.563 YYY[3585:a0f] input changed
> 2010-10-29 21:31:06.564 YYY[3585:a0f] input changed
> 2010-10-29 21:31:06.564 YYY[3585:a0f] input changed
> 2010-10-29 21:31:06.565 YYY[3585:a0f] input changed
> 2010-10-29 21:31:06.567 YYY[3585:a0f] input changed
> 2010-10-29 21:31:06.568 YYY[3585:a0f] input changed
> 2010-10-29 21:31:06.568 YYY[3585:a0f] input changed
> 2010-10-29 21:31:06.568 YYY[3585:a0f] input changed
> YYY(3585,0x115944000) malloc: *** auto malloc[3585]: error: GC operation on unregistered thread. Thread registered implicitly. Break on auto_zone_thread_registration_error() to debug.
> 
> 2010-10-29 21:31:16.595 YYY[3585:a0f] -[NSRegion InputChange:]: unrecognized selector sent to instance 0x200609f60
> 2010-10-29 21:31:16.596 YYY[3585:a0f] -[NSRegion InputChange:]: unrecognized selector sent to instance 0x200609f60
> 2010-10-29 21:31:16.914 YYY[3585:a0f] -[NSRegion InputChange:]: unrecognized selector sent to instance 0x200609f60
> 2010-10-29 21:31:16.915 YYY[3585:a0f] -[NSRegion InputChange:]: unrecognized selector sent to instance 0x200609f60
> 
> 
> I have read the malloc errors come from problems with the threading in the c libraries but I am not sure if there is anything I can do to fix this. Am I stuck with this error or is there a way around it?
> 
> This is definitely happening cause of attempting to access a shared memory pointer, between two or more threads.  What you need is a Mutex.  Which in the Objective-C / Cocoa world, is NSLock.  What NSLock does, is basically dis-allows access to a Variable Pointer, for reading and writing in other threads, while the Lock is in place.  It's not a catch all, save all for libraries that haven't been built thread aware, as some may still cause errors.  But if you use NSLock in your wrapper, in areas where you need to gather data from the C library for use in Objective-C / MacRuby, it should greatly reduce the malloc() crashes.
> 
> hth,
> 
> Mario
>  
> Thanks,
> 
> Shaun
> 
> 
> 
> 
> On 2010-10-24, at 12:17 AM, Mario Steele wrote:
> 
>> Hey Shaun,
>> 
>> On Sun, Oct 24, 2010 at 1:14 AM, Shaun August <shaun at eoslightmedia.com> wrote:
>> Hi Again,
>> 
>> I have spent some time with Nick's tutorial and I am looking into wrapping the framework I have been provided with and I have a few more questions. 
>> 
>> The framework I am working with is already compiled and not in an xcode file. I am guessing all I need to do is write the Objective-C wrapper?
>> 
>> Yeah, all you have to worry about, is writing the wrapper, as it is what I did with my code writing for my wrapper.
>>  
>> The other issue I am having is the framework only contains one header file which links out to a series of files contained in a system extension. 
>> 
>> The header file, is all you need to import, as it should define all public API Access you need, including structs, to interface with the binary library.
>>  
>> Am I able to write my wrapper in an Objective-C class from within my macruby application or do I need to create a bundle and import that into my project.
>> 
>> You will need to write Pure Objective-C code for the API Access, but the code can be included with your macruby application, in the project itself.  What will happen, is when you compile the main Objective C File, Main.m, it'll compile all the other Objective C files into the binary executable, that will eventually run your rb_main.rb file.  It'll give all the access of your Objective C libraries that you wrote, right within your application.
>> 
>> hth,
>> 
>> Mario
>>  
>> Thanks,
>> 
>> Shaun
>> 
>> 
>> 
>> 
>> On 2010-10-19, at 10:12 PM, Mario Steele wrote:
>> 
>>> Hello Shaun,
>>> 
>>> On Wed, Oct 20, 2010 at 12:00 AM, Shaun August <shaun at eoslightmedia.com> wrote:
>>> Hi Robert,
>>> 
>>> Thanks! That make sense. All those cocoa classes have capitals and they work fine. I seem to be able to call from the framework but I am running into an undefined method 'extern'. I have also read somewhere that extern doesn't work with MacRuby. Is that correct? The framework I am dealing with is calling C functions and I am having trouble accessing them.
>>> 
>>> extern is a macro for the ld library, to export an API Address in the Dynamic library, that can be access dynamically at run time.  With Objective-C, or more so specifically with MacRuby, you need to write a sort of thin wrapper around C Functions in Objective C, in order to access said Functions.  So if the framework that you are working with, doesn't expose Objective C API Functions, you'll need to write a thin wrapper in order to access them.  See the tutorial done by Nick Ludlam about wrapping the TagLib C Library, in Objective C.  You can find it here: http://www.macruby.org/documentation/reading-an-mp3-with-macruby.html  It's best to read through the entire article, as it gives you information about wrapping C Functions in Objective C, which is quite useful, when writing your own wrapper.
>>> 
>>> hth,
>>> 
>>> Mario
>>>  
>>> Thanks,
>>> 
>>> Shaun
>>> 
>>> 
>>> On 2010-10-19, at 8:26 PM, Robert Rice wrote:
>>> 
>>> > Hi Shaun:
>>> >
>>> > Lower case method names is only a convention. MacRuby will work fine using upper case, i.e., constant, method names.
>>> > I know because I only recently changed my project to conform to the convention.
>>> >
>>> > Bob Rice
>>> >
>>> >
>>> > On Oct 19, 2010, at 7:17 PM, Shaun August wrote:
>>> >
>>> >> Hi There,
>>> >>
>>> >> I am attempting to work with a framework provided by a USB device manufacturer and all of their method names start with capitols and I am wondering about the easiest way to access these methods through macruby. I remember reading somewhere about fixing the constants in Obj-C but I cannot locate the information.
>>> >>
>>> >> Does anyone have any suggestions as to how to remedy this? Should I rename every command in the framework?
>>> >>
>>> >> Thanks,
>>> >>
>>> >> Shaun
>>> >>
>>> >> _______________________________________________
>>> >> MacRuby-devel mailing list
>>> >> MacRuby-devel at lists.macosforge.org
>>> >> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>>> >>
>>> >
>>> > _______________________________________________
>>> > MacRuby-devel mailing list
>>> > MacRuby-devel at lists.macosforge.org
>>> > http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>>> 
>>> _______________________________________________
>>> MacRuby-devel mailing list
>>> MacRuby-devel at lists.macosforge.org
>>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>>> 
>>> 
>>> 
>>> -- 
>>> Mario Steele
>>> Lieutenant Commander 3 
>>> XO - Geo 99
>>> XO - STO IFT Fleet
>>> http://www.trekfederation.com
>>> http://geo99.ruby-im.net
>>> 
>>> _______________________________________________
>>> MacRuby-devel mailing list
>>> MacRuby-devel at lists.macosforge.org
>>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>> 
>> 
>> _______________________________________________
>> MacRuby-devel mailing list
>> MacRuby-devel at lists.macosforge.org
>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>> 
>> 
>> 
>> 
>> -- 
>> Mario Steele
>> Lieutenant Commander 3 
>> XO - Geo 99
>> XO - STO IFT Fleet
>> http://www.trekfederation.com
>> http://geo99.ruby-im.net
>> 
>> _______________________________________________
>> MacRuby-devel mailing list
>> MacRuby-devel at lists.macosforge.org
>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
> 
> 
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
> 
> 
> 
> 
> -- 
> Mario Steele
> Lieutenant Commander 3 
> XO - Geo 99
> XO - STO IFT Fleet
> http://www.trekfederation.com
> http://geo99.ruby-im.net
> 
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20101030/1b0379cd/attachment-0001.html>


More information about the MacRuby-devel mailing list