NSData, CFData maximum length question
I am confused about different behavior I'm seeing with CFData and NSData.If I create a new project in XCode 4.3.1 as a Core Foundation command line tool, and enter the code below... #include CoreFoundation/CoreFoundation.h int main(int argc, const char * argv[]) { CFIndex length = (1ULL 30); fprintf(stderr, length = %ld\n,length); CFMutableDataRef data = CFDataCreateMutable(kCFAllocatorDefault, 0); CFDataSetLength(data, length); } it crashes with the error message below: length = 1073741824 test(2463) malloc: *** mmap(size=18446744071562067968) failed (error code=12) *** error: can't allocate region *** set a breakpoint in malloc_error_break to debug 2012-03-21 20:55:35.292 test[2463:403] Attempt to allocate -2147483648 bytes for NS/CFData failed. Maximum size: 4398046511103 Whereas, if I create a new project as a Foundation command line tool, and enter the code below, it runs without errors. #import Foundation/Foundation.h int main(int argc, const char * argv[]) { @autoreleasepool { CFIndex length = (1ULL 30); fprintf(stderr, length = %ld\n,length); NSMutableData *data = [NSMutableData dataWithCapacity:0]; [data setLength:length]; } return 0; } Both Xcode projects are created (the default) as 64 bit. So, why does the Core Foundation CFDataSetLength get the wrong length? I've searched the 64 bit transition guides and can't find any answers. Any suggestions would be greatly appreciated. Thanks, Philip ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: NSData, CFData maximum length question
Found something interesting. If I simply set the capacity to length instead of 0, then it runs without crashing. Could this be a bug in CFData? int main(int argc, const char * argv[]) { CFIndex length = (1ULL 30); fprintf(stderr, length = %ld\n,length); CFMutableDataRef data = CFDataCreateMutable(kCFAllocatorDefault, length); CFDataSetLength(data, length); } - Philip On Mar 21, 2012, at 10:26 PM, Jens Alfke wrote: On Mar 21, 2012, at 6:00 PM, Grandinetti Philip wrote: it crashes with the error message below: length = 1073741824 test(2463) malloc: *** mmap(size=18446744071562067968) failed (error code=12) That is bizarre — it happens to me too. 18446744071562067968 = 0x8000 … so it’s as though something doubled the length parameter, then sign-extended it to 64 bits, before passing it to malloc. Why, I have no idea. —Jens ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
NSMatrix, NSForm - addRow - why above and not below?
I'm running into a strange behavior with NSForm (and also NSMatrix). (1) Using interface builder (in Xcode 4.3.1) I place an NSForm in a window. I add a NSButton and wire it to an IBAction that sends addRow to the NSForm. - (IBAction) addRow:(id)sender { [form addRow]; [form sizeToCells]; } The problem is that the new row is added ABOVE the existing row, not below as it's supposed to. I thought this was a problem coming from somewhere else in my app, but I created a new project in Xcode and this happens even in the simplest app. I must be doing something stupid wrong, but I can't find it. Any suggestions would be appreciated. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: NSMatrix, NSForm - addRow - why above and not below?
Thanks Keary, It all makes sense.Deceptive is the word below in the reference guide, which I usually interpret using the direction of gravity. Philip On Mar 13, 2012, at 1:49 PM, Keary Suska wrote: On Mar 13, 2012, at 8:55 AM, Grandinetti Philip wrote: I'm running into a strange behavior with NSForm (and also NSMatrix). (1) Using interface builder (in Xcode 4.3.1) I place an NSForm in a window. I add a NSButton and wire it to an IBAction that sends addRow to the NSForm. - (IBAction) addRow:(id)sender { [form addRow]; [form sizeToCells]; } The problem is that the new row is added ABOVE the existing row, not below as it's supposed to. I thought this was a problem coming from somewhere else in my app, but I created a new project in Xcode and this happens even in the simplest app. I must be doing something stupid wrong, but I can't find it. Any suggestions would be appreciated. It is a bit deceptive, but the problem is not what you think. The row is in fact added at the bottom, but when the view is resized it is sized from the bottom left corner, instead of the top left, which is intuitive for humanity but not apparently for the founders of Cocoa. To get the behavior you expect you need to either flip the coordinate system of the enclosing view or move the view back as such: - (IBAction) addRow:(id)sender { NSRect originalFrame = [form frame]; [form addRow]; [form sizeToCells]; NSRect newFrame = [form frame]; CGFloat delta = newFrame.size.height - originalFrame.size.height; newFrame.origin.y -= delta; [form setFrame:newFrame]; } Note that -sizeToCells does not cause the view to be redrawn, so you will get drawing artifacts. You may use -selectTextAtIndex: to set focus on the new form field, which will avoid the drawing issues (maybe a little more functional than just calling -setNeedsDisplay:). HTH, Keary Suska Esoteritech, Inc. Demystifying technology for your home or business ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Static variables reset inside CFPlugin code
Sorry Jens, I read too quickly. The app is for the physical and engineering sciences, and will perform a least squares fit of experimental data to different physical models.The models for the experimental data will be in the plugins, and the user will likely only need to the load a few of the plugin models. In the host app (actually in one of it's static libraries dedicated to handling SI Units) is a static variable pointing to a singleton library (CFMutableSet) with a growing set of SI units available in the main app (and plugins). The units are flyweights, so I don't want multiple copies floating around the program. So, I can't really define the static variable pointing to my library in the plugin.It needs to be defined in the library that handles all the SI units. Is it still possible to tell the linker to export the static variable pointing to my SI Units library in such a situation? Thanks again, Philip On Jan 1, 2012, at 9:49 PM, Jens Alfke wrote: On Jan 1, 2012, at 2:21 PM, Grandinetti Philip wrote: Your suggestions make sense, although... option (a) doesn't work for me, since the static variables are being used before the plugin is loaded. They're not options, they're steps. You need to do all of them. If you can't put the variable in the plugin, I _think_ you can put it in the app, then export it from the app and link the plugin against the app. Apps don't normally export symbols, but I think you can set this up. Maybe you could explain what the plugin does and why it and the app need to share a variable? —Jens ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Static variables reset inside CFPlugin code
Hi Steve, You raise a fundamental issue that I admit I don't completely understand. What are the differences between linking against a static library versus a framework. If I turned all my static libraries into a framework would the plugin see the static variables in the framework without having to do all the Xcode linking magic? Thanks, Philip On Jan 2, 2012, at 10:26 AM, Steve Sisak wrote: At 9:47 AM -0500 1/2/12, Grandinetti Philip wrote: So, I can't really define the static variable pointing to my library in the plugin.It needs to be defined in the library that handles all the SI units. Hi Philip, This is dusting off brain cells that I haven't used in awhile but, assuming all the plugins run only in your application, you could put all of the code/data shared by the plugins into a framework that resides in your application bundle. Your application and your plugins then link against this framework. You could use either CFPlugin or NSBundle as Chris suggests. The tough part here is all the Xcode magic to get the targets set up correctly -- I'm currently working out something similar, and will post results. If someone has a recipe for setting this up in Xcode 4 and would be willing to post it, I'd be interested as well. HTH, -Steve ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Static variables reset inside CFPlugin code
Hi Jens, Thanks for your patience. I'm getting a better understanding. Also, it helped a bit reading through ... http://developer.apple.com/library/mac/#documentation/developertools/conceptual/MachOTopics and http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/DynamicLibraries However, I'm still not sure what is the right way to do this. I have made the pointer global (no long declared as static),... CFMutableSetRef unitsLibrary = NULL; in my Units static library. The main app begins using the Units library at startup and starts filling this set. Then, eventually the plugin gets loaded. In the plugin factory function I added ... CFBundleRef bundle = CFBundleGetMainBundle(); CFMutableSetRef *pointer = CFBundleGetDataPointerForName (bundle,CFSTR(unitsLibrary)); PSUnitSetLibrary(*pointer); and this works! The plugin now knows about all the previous units that were defined in the main app. But I'm troubled by this solution. If I set debugger break points in the Units library the debugger no longer stops at those breakpoints when called by the plugin, but it does stop at those breakpoints when called by the main app. Makes me think I have two copies of code for my static units library running after the plugin is loaded. Does that make sense? Philip On Jan 2, 2012, at 1:22 PM, Jens Alfke wrote: On Jan 2, 2012, at 7:52 AM, Grandinetti Philip wrote: You raise a fundamental issue that I admit I don't completely understand. What are the differences between linking against a static library versus a framework. If I turned all my static libraries into a framework would the plugin see the static variables in the framework without having to do all the Xcode linking magic? The difference is copying vs. referencing. A static library gets copied into the target at link time, whereas if you link with a dynamic library or framework, the linker just notes down what symbols are referenced, and at load time those get resolved to those symbols in the library. For your purposes, the difference is that in a static library, everything that links with it has its own private copy of the library's global variables. In a dylib/framework, everything that links with it shares its global variables. The latter is what you want. —Jens ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Static variables reset inside CFPlugin code
Thanks Jens. With more experimenting and reading around I've now got a better picture, and your reply confirmed that picture. I think my problem was even trickier, because I defined my static variable (actually a pointer to a array) in a static library linked to the main app, and the plugin is loaded by the static library, not the main app. Whew! Your suggestions make sense, although... option (a) doesn't work for me, since the static variables are being used before the plugin is loaded. option (b) I tried changing the static variable into a global, and putting the global outside of main in the app, but this didn't work for me. Not sure what I did wrong here. option (c) seems like the solution I need, but I couldn't find how to do this in Xcode. option (d) I'm not sure I understand. If I link the host app against the plugin doesn't that defeat the purpose of making it a plugin? Ultimately, a solution I put together was to add an interface function to the plugin that passed the pointer from the host app over to the plugin, and used that to set the plugin's copy of the static variable (pointer in this case). If you know where I can read more about implementing option (c) or other suggestions I would be grateful to hear about it. Thanks, and happy new year! Philip On Jan 1, 2012, at 3:42 PM, Jens Alfke wrote: On Dec 29, 2011, at 12:11 PM, Grandinetti Philip wrote: I'm loading and creating a CFplugin using Core Foundation and running into a strange behavior where all the static variables defined throughout my code get reset to their initialization values when running code inside plugin. If you have two binaries — an application and a plugin — then each of those has independent global/static variables, even if those variables are declared in the same source files. The scope of globals is basically defined by the linker, and you've linked two independent binaries that have no magic connection to each other. If you want the two to share a variable, you'll need to: (a) define it in the plugin code, not the app (b) declare it as global not static, so it's visible outside that compilation unit (c) tell the linker to export that global from the plugin, e.g. by using an export file (d) make the app link against the plugin so it can access the variable —Jens ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Static variables reset inside CFPlugin code
Hi Chris, I prefer sticking with C for this part of my code. I just assumed that CFPlugin would be less work, but since you suggested CFBundle, I can see that it might be just as easy/hard. What would be the advantages? Either way, I guess I would still have a problem syncing up static variables once the bundle executable was loaded? Thanks, Philip On Jan 1, 2012, at 7:01 PM, Chris Hanson wrote: I have to wonder why you're using CFPlugIn in the first place. I strongly recommend using NSBundle and Objective-C for your plug-in architecture, or plain CFBundle and functions structs if you absolutely must use C rather than objects for your plug-in architecture. -- Chris ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Static variables reset inside CFPlugin code
I'm loading and creating a CFplugin using Core Foundation and running into a strange behavior where all the static variables defined throughout my code get reset to their initialization values when running code inside plugin. For example, I define a static variable and functions in a file foo.c static int foo = 0; void setFoo(void) { foo = 42;} void printFoo(void) {fprintf(stderr, foo is %d\n,foo);} then I install and call the plugin code using printFoo();// - prints 0 setFoo(); // - sets foo to 42 printFoo();// - prints 42 PluginInterfaceRef *iunknown = CFPlugInInstanceCreate(kCFAllocatorDefault, factoryID, kMyPluginTypeUUID); printFoo(); // - prints 42 which looks okay, but inside the factory function, called by CFPlugInInstanceCreate,... void *myFactory(CFAllocatorRef allocator, CFUUIDRef typeID) { if(!CFEqual(typeID, kMyPluginTypeUUID)) return NULL; MyPluginRef result = MyPluginCreate(typeID); printFoo(); // - prints 0 ?? return result; } printFoo() returns 0, and not 42. I'm using Xcode 4.2.1, and all the functions appear to be running in the main thread. Any ideas why this is happening, and suggestions for making static variables available inside the plugin? ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com