Xcode "Thread 1: EXC_BAD_ACCESS" error

  • Thread starter Thread starter hilman
  • Start date Start date
  • Tags Tags
    Error
AI Thread Summary
The discussion revolves around troubleshooting an EXC_BAD_ACCESS error encountered while processing bitmap images in a programming project. The issue arises when the program runs successfully on Mac Terminal and Windows but fails in Xcode. Key points include the importance of correctly opening files, as evidenced by the NULL pointer error when attempting to access the file. Suggestions include using debugging tools in Xcode to identify issues, such as checking the file path and ensuring the file is accessible. A solution was proposed to add error handling for file opening, which resolved the issue by ensuring the program only attempts to read from the file if it is successfully opened. Additionally, creating a configuration file to define file paths was recommended for better management of file locations in the project. Overall, the conversation emphasizes the necessity of proper file handling and debugging techniques in programming.
hilman
Messages
17
Reaction score
0
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.
 
Technology news on Phys.org
try increasing the size of the header array. It may be that the header info you're reading is larger than the array allows.
 
jedishrfu said:
try increasing the size of the header array. It may be that the header info you're reading is larger than the array allows.
Nope. Still same error displaying.
 
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 let's 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 let's 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:
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.
 
  • Like
Likes D H and jedishrfu
newjerseyrunner said:
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 let's 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 let's 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:
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.
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.
 
Between, I have added this line
Code:
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?
 
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.
 
  • Like
Likes jedishrfu
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
 
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?
 
  • #10
hilman said:
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?
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:
#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:
#include "config.h"

int main(int c, char** v){
    std::string myfilename = files::main_directory() + "myfile.txt";
}
 
Back
Top