MacOS Swift and dylib: pointer being freed was not allocated

hello, I am using dylibs and using c library.

I am opening the dylibs and using some of the function properly. However, some of the other functions are giving me errors relating to freeing pointers. here is one of the function below.

first I open the dl and save it as variable in a class    

 let dylib = dlopen("x.dylib", RTLD_NOW)

then i create type alias for the function

    typealias LoadConfiguration = @convention(c)  (ath_conf_t)> -> Void

then here is the function to call the function of c

    func LoadConfigLib(athConfT : inout ath_conf_t) -> Void?{

        guard dylib != nil  else {

            errorDylib()

            return nil

        }

        guard let sym = dlsym(dylib, "LoadConfiguration")  else {

            errorDylib()

            return nil

        }

        let f = unsafeBitCast(sym, to: LoadConfiguration.self)

        let result: Void = f(athConfT)

        print(result)

        return result

    }

here is the error.

malloc: *** error for object 0x102665cc0: pointer being freed was not allocated
malloc: *** set a breakpoint in malloc_error_break to debug things 

I am not certain how to fix this problem or how to approach this. any sorts of guidance would be appreciated

here is the ath_conf_t

public class ath_conf_t : NSObject {

    public var version: CInt

    public var channels: NSArray

    init(version: CInt = 0, channels: NSArray = Array(repeating: ath_channel_t.init(arg1: false, arg2: 0, adaptiveHigh: 0, arg3: 0), count: constant) as NSArray) {

        self.version = version

        self.channels = channels

    }

}
Answered by DTS Engineer in 737226022

The code you posted looks reasonable [1]. If you set a breakpoint on malloc_error_break, what does the backtrace look like?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] The only thing that doesn’t make sense to me is why the athConfT parameter is inout.

Accepted Answer

The code you posted looks reasonable [1]. If you set a breakpoint on malloc_error_break, what does the backtrace look like?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] The only thing that doesn’t make sense to me is why the athConfT parameter is inout.

I have found that it was due to the fact that the library contained a variable that it access. I commented the line that was freeing the memory (which is used on Windows version of the library) and it does work. However, I do have a different problem with the function using

ath_motor_mappings_t& out

I have no idea how to make proper convention for this parameter.

the function is

void Merge(        ath_motor_mappings_t a,        ath_motor_mappings_t b,        ath_motor_mappings_t& out)

I have tried using

    typealias Merge = @convention(c) (ath_motor_mappings_t, ath_motor_mappings_t, UnsafeMutablePointer<ath_motor_mappings_t>) -> Void
    typealias Merge = @convention(c) (ath_motor_mappings_t, ath_motor_mappings_t, ath_motor_mappings_t) -> Void

but both gives me EXC_BAD_ACCESS error. is there a convention that can specify reference?

ath_motor_mappings_t& is a C++ reference and Swift doesn’t (yet [1]) integrate with C++. You might be able to make this work for a specific platform by figuring out how to represent the C++ calling conventions in C but that’s not something I recommend. Rather, tweak your C++ code to expose this functionality via a C interface.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Although that’s on the cards.

MacOS Swift and dylib: pointer being freed was not allocated
 
 
Q