Exc Bad Access While Creating a New Characterset

EXC BAD ACCESS while creating a new CharacterSet

That looks like a bug to me. Building the difference of any two
CharacterSets results in an "infinite" recursion and a stack overflow.
Here is a minimal example which causes the crash:

let cs1 = CharacterSet.punctuationCharacters
let cs2 = CharacterSet.decimalDigits
let cs = cs1.subtracting(cs2)

A workaround is to use the

public mutating func remove(charactersIn string: String)

method of CharacterSet instead:

var punctuationCharactersExcludingHyphen = CharacterSet.punctuationCharacters
punctuationCharactersExcludingHyphen.remove(charactersIn: "-")

or if you want an extension method:

extension CharacterSet {
func subtracting(charactersIn string: String) -> CharacterSet {
var cs = self
cs.remove(charactersIn: string)
return cs
}
}

EXC_BAD_ACCESS when initialising an NSString

unichar isn't an ObjC object, it's a primitive C type (or typedef) for a single unicode character. You need to call the NSString method (which can take multiple characters in a C-like array) like this:

NSString * codedLetter = [NSString stringWithCharacters:&ch length:1];

(There are two problems in your line of code: first that you're passing the value of a unichar as the first argument, instead of a pointer to it, and the second that you're trying to call -length on something that's not an ObjC object, or even a pointer. It's blowing up due the latter when it tries to send a message to some random memory address.)

How to properly solve the EXC_BAD_ACCESS (code=EXC_i386_GPFLT) error in my function

If you want to handle it that way, instead of making a character set out of your string and checking if it's a subset you should use string's method rangeOfCharacter(from: CharacterSet) -> Range<String.Index>?. This method returns a position of a character from given set or nil if there was no character from the set present.

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
print(string)
let allowedCharacters = CharacterSet(charactersIn: "0123456789")
return string.rangeOfCharacter(from: allowedCharacters) != nil
}

However it is not a good approach to specify allowed characters as here you'll prevent user from removing characters with backspace. You should rather specify disallowed characters then this function will change to:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
print(string)
let disallowedCharacters = CharacterSet(charactersIn: "0123456789")
return string.rangeOfCharacter(from: disallowedCharacters) == nil
}

EXC_BAD_ACCESS When Trying to Perform a strcat Function in C

You're trying to modify a constant string. The first argument of strcat is also the destination string and in your case it is a constant string. You should use something like:

char s[100];
strcpy(s, "md5 ");
strcat(s, "blah");

Objective-C crash with EXC_BAD_ACCESS after reading a file

Based on reading your code only

The function call to fgetln() returns in length the number of characters in the line.

A C-array of characters with n elements is declared as char a[n] and the elements are addressed as a[0] to a[n-1].

You need to store length characters plus 1 for the EOS, you allocate an array of capacity length - 1 which is 2 too short.

Your strncpy() then writes past the end of the array.

Finally you write the EOS at index length - 1 when then maximum index is length - 2.

You are only overwriting by 1 byte (you write the EOS over the last character of the line), but that is enough to clobber whatever is next to the array on the stack (which might be cLine...)

HTH

BAD_ACCESS_ERR in a simple string copy function in c

char* dst = "AAAAAAAAAAAA";

You define destination to point to a section of memory that could be read only -- string literals are allowed by the C standard to be kept into a read only segment. 6.4.5p6 String literals:


  1. ... If the program attempts to modify such an array, the behavior is undefined.

You need to define the destination in heap or into a vector, that you are sure to allocate correctly.

dst = malloc(SIZE);

or

char dst[100];

In the first case make sure you call free, in the second case make sure you do not access it after its scope or the source does not have more than 99 characters+NUL.



Related Topics



Leave a reply



Submit