Xcode "Thread 1: EXC_BAD_ACCESS" error

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

Discussion Overview

The discussion revolves around troubleshooting a "Thread 1: EXC_BAD_ACCESS" error encountered in Xcode while working on an image processing program. Participants explore potential causes and solutions related to file handling and debugging techniques, with a focus on the differences in behavior between running the program in Xcode versus other environments like Mac Terminal and Windows.

Discussion Character

  • Technical explanation
  • Debugging techniques
  • Exploratory

Main Points Raised

  • One participant notes that the error occurs despite the program running correctly in other environments, suggesting a potential issue with file handling in Xcode.
  • Another participant proposes increasing the size of the header array to accommodate larger header information, but this suggestion does not resolve the issue.
  • Several participants emphasize the importance of using breakpoints and debugging tools in Xcode to identify issues, particularly pointing out that a NULL file pointer indicates the file could not be opened.
  • A participant suggests ensuring the correct file path is specified, as Xcode may not compile the code in the same directory as the source files.
  • One participant reports success after adding error handling for file opening, indicating that the program works when the file is correctly accessed.
  • Another participant clarifies that the absence of an else statement in the code led to the program not crashing but not handling the error properly.
  • Participants discuss the need to specify file locations explicitly and suggest creating a configuration file to manage file paths more effectively.

Areas of Agreement / Disagreement

Participants generally agree on the importance of proper file handling and debugging techniques, but there is no consensus on the best approach to resolve the initial error, as multiple suggestions and perspectives are presented.

Contextual Notes

There are limitations regarding the assumptions about file paths and the specific configurations in Xcode that may affect how the program accesses files. The discussion does not resolve the underlying causes of the EXC_BAD_ACCESS error, leaving some uncertainty about the best practices for file handling in this context.

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
5K
  • · Replies 15 ·
Replies
15
Views
5K