Any idea why this fails (dylib fopening file in app bundle)

Discussion in 'Mac Programming' started by Cromulent, Feb 10, 2010.

  1. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #1
    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.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    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.
     
  3. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    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.
     
  4. Cromulent thread starter macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #4
    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.
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    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.
     
  6. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #6
    I'm afraid everyones irritation with farmerdoug is spilling over and affecting their response to others.
     
  7. Cromulent thread starter macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #7
    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.
     

Share This Page