Xcode "Thread 1: EXC_BAD_ACCESS" error

  • Thread starter hilman
  • Start date
  • Tags
    Error
In summary, XCode is not able to locate the file that you are trying to compile into. You need to tell the Xcode where the file is located.
  • #1
hilman
17
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
  • #2
try increasing the size of the header array. It may be that the header info you're reading is larger than the array allows.
 
  • #3
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.
 
  • #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 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
  • #5
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.
 
  • #6
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?
 
  • #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.
 
  • Like
Likes jedishrfu
  • #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
 
  • #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?
 
  • #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";
}
 

1. What does "Thread 1: EXC_BAD_ACCESS" error mean in Xcode?

The "Thread 1: EXC_BAD_ACCESS" error in Xcode is a common error that occurs when a memory access violation happens in a thread. This means that the program is trying to access a memory address that is not allowed, resulting in a crash.

2. Why does the "Thread 1: EXC_BAD_ACCESS" error occur in Xcode?

There can be several reasons for the "Thread 1: EXC_BAD_ACCESS" error in Xcode. Some common causes include accessing a released object, accessing an invalid pointer, or accessing a variable that has been deallocated.

3. How can I fix the "Thread 1: EXC_BAD_ACCESS" error in Xcode?

To fix the "Thread 1: EXC_BAD_ACCESS" error, you need to identify the root cause of the error. This can be done by analyzing the stack trace or using Xcode's debugging tools. Once you have identified the cause, you can make the necessary changes in your code to resolve the error.

4. Can low memory on my device cause the "Thread 1: EXC_BAD_ACCESS" error in Xcode?

Yes, low memory on your device can also lead to the "Thread 1: EXC_BAD_ACCESS" error in Xcode. When the device has low memory, the operating system may terminate some of the running processes, leading to the error. It is recommended to test your app on a device with sufficient memory to avoid this error.

5. Is the "Thread 1: EXC_BAD_ACCESS" error a fatal error in Xcode?

Yes, the "Thread 1: EXC_BAD_ACCESS" error is considered a fatal error in Xcode. This means that it will cause your program to crash and terminate. It is important to identify and fix this error to ensure a smooth and stable operation of your app.

Similar threads

  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
1
Views
617
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
6
Views
4K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
Back
Top