Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT) is causing my scanf to fail
You're taking the size of your array in bytes, not in the number of elements. Do this instead:
for( int i = 0; i < sizeof(nums) / sizeof(nums[0]); i++ )
{
FATAL ERROR: Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT). My code won't run
In your code the loop value would become 3 after two iterations.
int loops = 1;
for(int x = 1; x < 50; x++){
loops++;
//...
}
Now in this part of the code you iterate 3 times:
for (int x = 0; x < loops; x++) {
std::string name = organiser.top();
organiser.pop();
//...
}
So you pop the stack 3 times while there are only 2 elements.
When trying to print from string getting error: Exception: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
You are writing to unallocated memory. That invokes Undefined Behaviour and from that point anything can happen at any moment:
int* extractNums(char* nums)
{
char *copiedStr, *token; // copiedStr is an initialized pointer here
int *newArr=NULL,counter = 0, intChar;
char sep[] = " ";
strcpy(copiedStr,nums); // use of the initialized pointer: UB!
Why EXC_BAD_ACCESS (code=EXC_I386_GPFLT) when callback pointer to function?
Running under gdb
, after the fault, doing a tb
command to get a stack traceback, we see:
#0 0x00000005004016e6 in ?? ()
#1 0x000000000040163a in HashSetEnter (h=0x7fffffffdc10,
elemAddr=0x7fffffffdc40) at orig.c:150
#2 0x0000000000401834 in add_docfreq (wi=0x405260, docid=1611742826915764000,
frequency=5) at orig.c:266
#3 0x0000000000401879 in main () at orig.c:278
(gdb) frame 1
#1 0x000000000040163a in HashSetEnter (h=0x7fffffffdc10,
elemAddr=0x7fffffffdc40) at orig.c:150
150 int hash = h->hashfn(elemAddr, h->bucketNum);
You are segfaulting in HashSetEnter
, at the line:
int hash = h->hashfn(elemAddr, h->bucketNum);
This is because h
is not valid at this point.
Examinining the source, the place that sets the value that is ultimately invalid, it is set in new_wordindex
.
In new_wordindex
, you are saving [and returning] the address of h
.
h
is a function scoped variable here, so it is no longer valid after the function returns.
You have to use malloc
for this. And, later, you need to be able to free
this pointer during cleanup.
Here's the refactored code for the incorrect function.
Note that to show old/original code vs. new/corrected code, I'm using preprocessor conditionals:
#if 0
// old/original code
// NOTE: this is _not_ compiled in
#else
// new/corrected code
// NOTE: this _is_ compiled in
#endif
The code under #if 0
can be elided/removed, leaving just the #else
code.
static void
new_wordindex(word_index * wi, const char *word)
{
// NOTE/BUG: h is function scoped -- this can _not_ be saved and returned
// because it ceases to be valid when we return
#if 0
hashset h;
HashSetNew(&h, sizeof(doc_freq), kWordIndexHashSetBuckets, hash_docfreq, comp_docfreq, NULL);
wi->freqs = &h;
#else
hashset *h = malloc(sizeof(*h));
HashSetNew(h, sizeof(doc_freq), kWordIndexHashSetBuckets, hash_docfreq, comp_docfreq, NULL);
wi->freqs = h;
#endif
size_t wordlen = strlen(word);
wi->word = (char *) malloc(wordlen + 1); // +1 for null-termination
strcpy(wi->word, word);
(wi->word)[wordlen] = '\0';
}
Getting error Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT) when loading AVPlayer
I've made a guide that hopefully shows what you've been misunderstanding.
I apologize to other readers for not using MARKDOWN text format, but please do consider this as my upmost attempt for the original poster. We've already went through sufficient discussions in the previous post.
So, how do you fix this?
I guess there's nothing good to give you codes directly, please try to fix the code based on this information.
And if you have more questions, I'll try my best to help you :)
Related Topics
What Is the Performance Penalty of C++11 Thread_Local Variables in Gcc 4.8
Get Computer Name and Logged User Name
/Usr/Lib64/Libstdc++.So.6: Version 'Glibcxx_3.4.15' Not Found
How to Get Memory Information on Linux System
What Is the Easiest Way to Parse an Ini File in C++
Selecting a Member Function Using Different Enable_If Conditions
Range-For-Loops and Std::Vector<Bool>
Problems with Std::Stoi, Not Working on Mingw Gcc 4.7.2
How to Find and Replace String
How to Build a Graphical User Interface in C++
How to Break Out of a Loop from Inside a Switch