Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Cromulent

macrumors 604
Original poster
Oct 2, 2006
6,824
1,126
The Land of Hope and Glory
I had a little bug today that caused me to scratch my head for a bit. It turned out that if you use NSBundle followed by NSString UTF8String to get a path for a resource in your app bundle and convert it to a char * and then call fopen in a dynamic library you are linking too it results in EXEC_BAD_ACCESS but calling fopen locally is fine.

I've fixed the problem but I'd like some ideas why this causes errors. I ran the debugger and it said that it had crashed on a call to flockfile in fopen which is documented here: http://www.opengroup.org/onlinepubs/000095399/functions/flockfile.html which seems to suggest a locking issue. But then that is even harder to understand than what I assume to be a permissions error of some sort.

Unless code from a dynamic library runs in a separate process and thus locks files it has opened so passing a FILE * back results is nasty things happening.

Any ideas?

Edit: Ah, on closer inspection it seems that it is closer to the last point. Strange, I had never heard that was a problem before. I guess I should have worked it out though. Oh well.
 
I had a little bug today that caused me to scratch my head for a bit. It turned out that if you [mumble mumble description of code mumble mumble].

Post your code.

It turns out that if you post your code, then we have some chance of debugging your code. If you only post descriptions of code, which may or may not maintain a strict one-to-one isomorphism with said code, then the statistical probability of anyone being able to debug the (possibly non-isomorphic) description approaches zero as the inverse square of the number of descriptive words.

I hope I've made things perfectly clear.
 
A random guess: UTF8String returns a C-string that is owned by the NSString object, so when that object deallocates, it also frees the C-string. You would need to copy it if you're using the C-string outside of the scope of the object.
 
It turns out that if you post your code, then we have some chance of debugging your code.

It doesn't need debugging.

In fact I just realised that I explained it wrong anyway. It doesn't crash on fopen it crashes in fgets, more specifically in a flockfile which I assume is called somewhere in fgets.

No need to worry about it though. I was just somewhat interested in it and wondered if there was something obvious I had missed.
 
It doesn't need debugging.

In fact I just realised that I explained it wrong anyway [emphasis added]. It doesn't crash on fopen it crashes in fgets, more specifically in a flockfile which I assume is called somewhere in fgets.

No need to worry about it though. I was just somewhat interested in it and wondered if there was something obvious I had missed.

I could not have asked for a better illustration of my point.

You're asking for an explanation of code that no one else but you has seen, based solely on the wrong description of it. Are you honestly expecting us to reverse engineer exactly the code you had written, and then somehow replicate the problem you had with it, in order to explain the cause of this problem?

The most obvious thing you've missed is that we have no way of replicating your code, much less testing it to see if it exhibits the problems you see. Different programmers given the identical task, can and will code it in different ways, even if the task is relatively trivial and extremely well specified. This is inherent to the nature of engineering, and even more so to reverse engineering.

So if you honestly want even the forlorn hope of an explanation, post your code and see if someone else can replicate the problem. Otherwise, accept it as an unfathomable mystery and pretend it never happened.
 
You're asking for an explanation of code that no one else but you has seen, based solely on the wrong description of it. Are you honestly expecting us to reverse engineer exactly the code you had written, and then somehow replicate the problem you had with it, in order to explain the cause of this problem?

Calm down. As I said it was only an idle enquiry. No fuss if I don't get an explanation.

Edit: Anyway the bug just showed that my initial design choice was a bad move in the first place. Fixing it lead me to fix a much bigger problem with the code.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.