Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Xcode "Thread 1: EXC_BAD_ACCESS" error

  1. Oct 21, 2015 #1
    Screen Shot 2015-10-21 at 7.22.46 PM.png



    So, I was making this simple image processing programming for the bitmap image. Like the image I upload here, that Thread 1: EXC_BAD_ACCESS occurred although the program can perfectly running when I run it on Mac Terminal and on Windows. What seems to be the problem? Since my English is not my first language and especially I am not good in computer-related english language, I hope someone can explain to me easily. Plus, I am still a beginner in programming although I am already deeply falling in love with it.
     
  2. jcsd
  3. Oct 21, 2015 #2

    jedishrfu

    Staff: Mentor

    try increasing the size of the header array. It may be that the header info you're reading is larger than the array allows.
     
  4. Oct 21, 2015 #3
    Nope. Still same error displaying.
     
  5. Oct 21, 2015 #4
    Read up on how to use breakpoints and some of the debugging tools, your code is correct, but I see your problem in the very bottom left of the image you posted. Whenever XCode notices that you did something wrong, it automatically pauses your code and lets you investigate, this is called the debugger. The variable viewer is very important to figure out how to use, it's on the lower left of your screenshot, it lets you see what your variables are.

    I see that fp is NULL. That means that your file was not able to be opened. Add this code.

    Code (Text):
    fp = fopen(input_file, "rb");
    if (fb == NULL){
        printf("%s was not able to be opened", input_file);
        exit(1);
    }
    Here is my guess as to what's happening. Are you specifying the exact location of your file, or are you expecting it to know that the file is in the same directory as your code? XCode does not compile your code into the same directory, it does this because it's an advanced tool and has the ability to compile the same code in different ways into different folders.
     
  6. Oct 22, 2015 #5

    Actually I am expecting the Xcode to know that the file is the same directory as my code. So, How should I fix this? I am really a beginner so pardon me.
     
  7. Oct 22, 2015 #6
    Between, I have added this line
    Code (Text):
    if (fp) { fread(header,1,54,fp);            
    fread(img_work,1,width*height*3,fp);
    fclose(fp); }
    and the code suddenly works (I still didn't test until the end of the code yet). Why with that?
     
  8. Oct 22, 2015 #7
    It works because you caught the error and handled it correctly. Not crashing is not the same as it not working. XCode is just a tool, it's up to you as the programmer to tell the code where to find your file. Unfortunately, there is no real easy way to do that, I would use another #define for it.
     
  9. Oct 22, 2015 #8
    It worked because you did not put else statement for the if. That is, your code is saying "if the file is opened then read from it otherwise do nothing. So something like this:

    if (fp) {
    fread(header,1,54,fp);
    fread(img_work,1,width*height*3,fp);
    fclose(fp);
    } else {
    printf("%s was not able to be opened", input_file);
    };

    then you should see the error message
     
  10. Oct 28, 2015 #9
    Thank you guys! Although I am still not very sure what is happening, I do get the picture. So, I think I should tell the Xcode where to locate the file first?
     
  11. Oct 28, 2015 #10
    Yes, I do this in my own projects.

    I usually create a file that I call config.h I put constants I need, like the directories of files in there and then just include it wherever I need them.

    Code (Text):

    #ifndef _CONFIG_H_
    #define _CONFIG_H_

    #include <string>
    struct files {
         inline static std::string main_directory(void){ return "/my/directory/to/the/files"; }
    };

    #endif
    Then when you load your file
    Code (Text):
    #include "config.h"

    int main(int c, char** v){
        std::string myfilename = files::main_directory() + "myfile.txt";
    }
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Xcode "Thread 1: EXC_BAD_ACCESS" error
  1. Xcode help (Replies: 6)

  2. Std in Xcode (Replies: 2)

Loading...