Xcode "Thread 1: EXC_BAD_ACCESS" error

  • Thread starter Thread starter hilman
  • Start date Start date
  • Tags Tags
    Error
Click For Summary
SUMMARY

The discussion centers around resolving the "Thread 1: EXC_BAD_ACCESS" error encountered in Xcode while processing bitmap images. The primary issue identified is that the file pointer (fp) is NULL, indicating that the file cannot be opened. To fix this, users are advised to specify the exact file path and implement error handling when opening files. Additionally, utilizing Xcode's debugging tools, such as breakpoints and the variable viewer, is crucial for diagnosing issues effectively.

PREREQUISITES
  • Understanding of C programming and file handling
  • Familiarity with Xcode IDE and its debugging tools
  • Basic knowledge of bitmap image processing
  • Experience with error handling in C
NEXT STEPS
  • Learn how to implement error handling in C using fopen and conditional statements
  • Explore Xcode's debugging features, including breakpoints and the variable viewer
  • Research best practices for managing file paths in C projects
  • Study bitmap image processing techniques and libraries in C
USEFUL FOR

Beginner programmers, C developers working with file I/O, and anyone troubleshooting image processing applications in Xcode.

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   Reactions: 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   Reactions: 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";
}
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 4 ·
Replies
4
Views
7K
Replies
4
Views
8K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 15 ·
Replies
15
Views
5K