Skip to content

Commit

Permalink
sdl: replace SDL_free() with free() for variables that were created u…
Browse files Browse the repository at this point in the history
…sing C.CString()

Signed-off-by: Lilis Iskandar <[email protected]>
  • Loading branch information
veeableful committed Dec 2, 2023
1 parent 51ffa7e commit 7ed18a0
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 40 deletions.
8 changes: 4 additions & 4 deletions sdl/audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func GetAudioDriver(index int) string {
// (https://wiki.libsdl.org/SDL_AudioInit)
func AudioInit(driverName string) error {
_driverName := C.CString(driverName)
defer C.SDL_free(unsafe.Pointer(_driverName))
defer C.free(unsafe.Pointer(_driverName))
if C.SDL_AudioInit(_driverName) != 0 {
return GetError()
}
Expand Down Expand Up @@ -403,7 +403,7 @@ func OpenAudioDevice(device string, isCapture bool, desired, obtained *AudioSpec
if device == "" {
_device = nil
}
defer C.SDL_free(unsafe.Pointer(_device))
defer C.free(unsafe.Pointer(_device))
if id := AudioDeviceID(C.SDL_OpenAudioDevice(_device, C.int(Btoi(isCapture)), desired.cptr(), obtained.cptr(), C.int(allowedChanges))); id > 0 {
return id, nil
}
Expand Down Expand Up @@ -454,8 +454,8 @@ func LoadWAVRW(src *RWops, freeSrc bool) ([]byte, *AudioSpec) {
func LoadWAV(file string) ([]byte, *AudioSpec) {
_file := C.CString(file)
_rb := C.CString("rb")
defer C.SDL_free(unsafe.Pointer(_file))
defer C.SDL_free(unsafe.Pointer(_rb))
defer C.free(unsafe.Pointer(_file))
defer C.free(unsafe.Pointer(_rb))

var _audioBuf *C.Uint8
var _audioLen C.Uint32
Expand Down
2 changes: 1 addition & 1 deletion sdl/clipboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "unsafe"
// (https://wiki.libsdl.org/SDL_SetClipboardText)
func SetClipboardText(text string) error {
_text := C.CString(text)
defer C.SDL_free(unsafe.Pointer(_text))
defer C.free(unsafe.Pointer(_text))
if C.SDL_SetClipboardText(_text) < 0 {
return GetError()
}
Expand Down
4 changes: 2 additions & 2 deletions sdl/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func GetBasePath() string {
func GetPrefPath(org, app string) string {
_org := C.CString(org)
_app := C.CString(app)
defer C.SDL_free(unsafe.Pointer(_org))
defer C.SDL_free(unsafe.Pointer(_app))
defer C.free(unsafe.Pointer(_org))
defer C.free(unsafe.Pointer(_app))
_val := C.SDL_GetPrefPath(_org, _app)
defer C.SDL_free(unsafe.Pointer(_val))
return C.GoString(_val)
Expand Down
6 changes: 3 additions & 3 deletions sdl/gamecontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ func (btn GameControllerButton) c() C.SDL_GameControllerButton {
// (https://wiki.libsdl.org/SDL_GameControllerAddMapping)
func GameControllerAddMapping(mappingString string) int {
_mappingString := C.CString(mappingString)
defer C.SDL_free(unsafe.Pointer(_mappingString))
defer C.free(unsafe.Pointer(_mappingString))
return int(C.SDL_GameControllerAddMapping(_mappingString))
}

Expand Down Expand Up @@ -555,7 +555,7 @@ func GameControllerUpdate() {
// (https://wiki.libsdl.org/SDL_GameControllerGetAxisFromString)
func GameControllerGetAxisFromString(pchString string) GameControllerAxis {
_pchString := C.CString(pchString)
defer C.SDL_free(unsafe.Pointer(_pchString))
defer C.free(unsafe.Pointer(_pchString))
return GameControllerAxis(C.SDL_GameControllerGetAxisFromString(_pchString))
}

Expand Down Expand Up @@ -587,7 +587,7 @@ func (ctrl *GameController) Axis(axis GameControllerAxis) int16 {
// (https://wiki.libsdl.org/SDL_GameControllerGetButtonFromString)
func GameControllerGetButtonFromString(pchString string) GameControllerButton {
_pchString := C.CString(pchString)
defer C.SDL_free(unsafe.Pointer(_pchString))
defer C.free(unsafe.Pointer(_pchString))
return GameControllerButton(C.SDL_GameControllerGetButtonFromString(_pchString))
}

Expand Down
2 changes: 1 addition & 1 deletion sdl/hidapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func HIDOpen(vendorID, productID uint16, _serialNumber *C.wchar_t) (device *HIDD
// (https://wiki.libsdl.org/SDL_hid_open_path)
func HIDOpenPath(path string, exclusive bool) (device *HIDDevice) {
_path := C.CString(path)
defer C.SDL_free(unsafe.Pointer(_path))
defer C.free(unsafe.Pointer(_path))
_exclusive := C.int(0)
if exclusive {
_exclusive = C.int(1)
Expand Down
12 changes: 6 additions & 6 deletions sdl/hints.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ func (hp HintPriority) c() C.SDL_HintPriority {
func SetHintWithPriority(name, value string, hp HintPriority) bool {
_name := C.CString(name)
_value := C.CString(value)
defer C.SDL_free(unsafe.Pointer(_name))
defer C.SDL_free(unsafe.Pointer(_value))
defer C.free(unsafe.Pointer(_name))
defer C.free(unsafe.Pointer(_value))
return C.SDL_SetHintWithPriority(_name, _value, hp.c()) > 0
}

Expand All @@ -353,24 +353,24 @@ func SetHintWithPriority(name, value string, hp HintPriority) bool {
func SetHint(name, value string) bool {
_name := C.CString(name)
_value := C.CString(value)
defer C.SDL_free(unsafe.Pointer(_name))
defer C.SDL_free(unsafe.Pointer(_value))
defer C.free(unsafe.Pointer(_name))
defer C.free(unsafe.Pointer(_value))
return C.SDL_SetHint(_name, _value) > 0
}

// ResetHint resets a hint to the default value.
// (https://wiki.libsdl.org/SDL_ResetHint)
func ResetHint(name string) bool {
_name := C.CString(name)
defer C.SDL_free(unsafe.Pointer(_name))
defer C.free(unsafe.Pointer(_name))
return C.SDL_ResetHint(_name) == C.SDL_TRUE
}

// GetHint returns the value of a hint.
// (https://wiki.libsdl.org/SDL_GetHint)
func GetHint(name string) string {
_name := C.CString(name)
defer C.SDL_free(unsafe.Pointer(_name))
defer C.free(unsafe.Pointer(_name))
return C.GoString(C.SDL_GetHint(_name))
}

Expand Down
4 changes: 2 additions & 2 deletions sdl/joystick.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ func JoystickGetDeviceInstanceID(index int) JoystickID {
func JoystickGetGUIDString(guid JoystickGUID) string {
_pszGUID := make([]rune, 1024)
pszGUID := C.CString(string(_pszGUID[:]))
defer C.SDL_free(unsafe.Pointer(pszGUID))
defer C.free(unsafe.Pointer(pszGUID))
C.SDL_JoystickGetGUIDString(guid.c(), pszGUID, C.int(unsafe.Sizeof(_pszGUID)))
return C.GoString(pszGUID)
}
Expand All @@ -514,7 +514,7 @@ func JoystickGetGUIDString(guid JoystickGUID) string {
// (https://wiki.libsdl.org/SDL_JoystickGetGUIDFromString)
func JoystickGetGUIDFromString(pchGUID string) JoystickGUID {
_pchGUID := C.CString(pchGUID)
defer C.SDL_free(unsafe.Pointer(_pchGUID))
defer C.free(unsafe.Pointer(_pchGUID))
return (JoystickGUID)(C.SDL_JoystickGetGUIDFromString(_pchGUID))
}

Expand Down
4 changes: 2 additions & 2 deletions sdl/keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func GetScancodeName(code Scancode) string {
// (https://wiki.libsdl.org/SDL_GetScancodeFromName)
func GetScancodeFromName(name string) Scancode {
_name := C.CString(name)
defer C.SDL_free(unsafe.Pointer(_name))
defer C.free(unsafe.Pointer(_name))
return (Scancode)(C.SDL_GetScancodeFromName(_name))
}

Expand All @@ -115,7 +115,7 @@ func GetKeyName(code Keycode) string {
// (https://wiki.libsdl.org/SDL_GetKeyFromName)
func GetKeyFromName(name string) Keycode {
_name := C.CString(name)
defer C.SDL_free(unsafe.Pointer(_name))
defer C.free(unsafe.Pointer(_name))
return (Keycode)(C.SDL_GetKeyFromName(_name))
}

Expand Down
4 changes: 2 additions & 2 deletions sdl/loadso.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ type SharedObject uintptr
// (https://wiki.libsdl.org/SDL_LoadObject)
func LoadObject(sofile string) SharedObject {
_sofile := C.CString(sofile)
defer C.SDL_free(unsafe.Pointer(_sofile))
defer C.free(unsafe.Pointer(_sofile))
return (SharedObject)(C.SDL_LoadObject(_sofile))
}

// LoadFunction returns a pointer to the named function from the shared object.
// (https://wiki.libsdl.org/SDL_LoadFunction)
func (handle SharedObject) LoadFunction(name string) unsafe.Pointer {
_name := C.CString(name)
defer C.SDL_free(unsafe.Pointer(_name))
defer C.free(unsafe.Pointer(_name))
return (unsafe.Pointer)(C.SDL_LoadFunction((unsafe.Pointer)(handle), _name))
}

Expand Down
16 changes: 8 additions & 8 deletions sdl/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func Log(str string, args ...interface{}) {
str = fmt.Sprintf(str, args...)

cstr := C.CString(str)
defer C.SDL_free(unsafe.Pointer(cstr))
defer C.free(unsafe.Pointer(cstr))

C._SDL_Log(cstr)
}
Expand All @@ -146,7 +146,7 @@ func LogVerbose(category LogCategory, str string, args ...interface{}) {
str = fmt.Sprintf(str, args...)

cstr := C.CString(str)
defer C.SDL_free(unsafe.Pointer(cstr))
defer C.free(unsafe.Pointer(cstr))

C._SDL_LogVerbose(C.int(category), cstr)
}
Expand All @@ -157,7 +157,7 @@ func LogDebug(category LogCategory, str string, args ...interface{}) {
str = fmt.Sprintf(str, args...)

cstr := C.CString(str)
defer C.SDL_free(unsafe.Pointer(cstr))
defer C.free(unsafe.Pointer(cstr))

C._SDL_LogDebug(C.int(category), cstr)
}
Expand All @@ -168,7 +168,7 @@ func LogInfo(category LogCategory, str string, args ...interface{}) {
str = fmt.Sprintf(str, args...)

cstr := C.CString(str)
defer C.SDL_free(unsafe.Pointer(cstr))
defer C.free(unsafe.Pointer(cstr))

C._SDL_LogInfo(C.int(category), cstr)
}
Expand All @@ -179,7 +179,7 @@ func LogWarn(category LogCategory, str string, args ...interface{}) {
str = fmt.Sprintf(str, args...)

cstr := C.CString(str)
defer C.SDL_free(unsafe.Pointer(cstr))
defer C.free(unsafe.Pointer(cstr))

C._SDL_LogWarn(C.int(category), cstr)
}
Expand All @@ -190,7 +190,7 @@ func LogError(category LogCategory, str string, args ...interface{}) {
str = fmt.Sprintf(str, args...)

cstr := C.CString(str)
defer C.SDL_free(unsafe.Pointer(cstr))
defer C.free(unsafe.Pointer(cstr))

C._SDL_LogError(C.int(category), cstr)
}
Expand All @@ -201,7 +201,7 @@ func LogCritical(category LogCategory, str string, args ...interface{}) {
str = fmt.Sprintf(str, args...)

cstr := C.CString(str)
defer C.SDL_free(unsafe.Pointer(cstr))
defer C.free(unsafe.Pointer(cstr))

C._SDL_LogCritical(C.int(category), cstr)
}
Expand All @@ -212,7 +212,7 @@ func LogMessage(category LogCategory, pri LogPriority, str string, args ...inter
str = fmt.Sprintf(str, args...)

cstr := C.CString(str)
defer C.SDL_free(unsafe.Pointer(cstr))
defer C.free(unsafe.Pointer(cstr))

C._SDL_LogMessage(C.int(category), C.SDL_LogPriority(pri), cstr)
}
Expand Down
4 changes: 2 additions & 2 deletions sdl/rwops.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ func (rwops *RWops) cptr() *C.SDL_RWops {
func RWFromFile(file, mode string) *RWops {
_file := C.CString(file)
_mode := C.CString(mode)
defer C.SDL_free(unsafe.Pointer(_file))
defer C.SDL_free(unsafe.Pointer(_mode))
defer C.free(unsafe.Pointer(_file))
defer C.free(unsafe.Pointer(_mode))
return (*RWops)(unsafe.Pointer(C.SDL_RWFromFile(_file, _mode)))
}

Expand Down
2 changes: 1 addition & 1 deletion sdl/system_android.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,6 @@ func IsAndroidTV() bool {
// (https://wiki.libsdl.org/SDL_AndroidShowToast)
func AndroidShowToast(message string, duration, gravity, xoffset, yoffset int) (err error) {
_message := C.CString(message)
defer C.SDL_free(unsafe.Pointer(_message))
defer C.free(unsafe.Pointer(_message))
return errorFromInt(int(C.SDL_AndroidShowToast(_message, C.int(duration), C.int(gravity), C.int(xoffset), C.int(yoffset))))
}
10 changes: 5 additions & 5 deletions sdl/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -948,9 +948,9 @@ func (window *Window) GetWindowOpacity() (opacity float32, err error) {
// (https://wiki.libsdl.org/SDL_ShowSimpleMessageBox)
func ShowSimpleMessageBox(flags MessageBoxFlags, title, message string, window *Window) error {
_title := C.CString(title)
defer C.SDL_free(unsafe.Pointer(_title))
defer C.free(unsafe.Pointer(_title))
_message := C.CString(message)
defer C.SDL_free(unsafe.Pointer(_message))
defer C.free(unsafe.Pointer(_message))
return errorFromInt(int(
C.SDL_ShowSimpleMessageBox(C.Uint32(flags), _title, _message, window.cptr())))
}
Expand All @@ -959,15 +959,15 @@ func ShowSimpleMessageBox(flags MessageBoxFlags, title, message string, window *
// (https://wiki.libsdl.org/SDL_ShowMessageBox)
func ShowMessageBox(data *MessageBoxData) (buttonid int32, err error) {
_title := C.CString(data.Title)
defer C.SDL_free(unsafe.Pointer(_title))
defer C.free(unsafe.Pointer(_title))
_message := C.CString(data.Message)
defer C.SDL_free(unsafe.Pointer(_message))
defer C.free(unsafe.Pointer(_message))

var cbuttons []C.SDL_MessageBoxButtonData
var cbtntexts []*C.char
defer func(texts []*C.char) {
for _, t := range texts {
C.SDL_free(unsafe.Pointer(t))
C.free(unsafe.Pointer(t))
}
}(cbtntexts)

Expand Down
2 changes: 1 addition & 1 deletion sdl/vulkan.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func VulkanLoadLibrary(path string) error {
ret = C.SDL_Vulkan_LoadLibrary(nil)
} else {
cpath := C.CString(path)
defer C.SDL_free(unsafe.Pointer(cpath))
defer C.free(unsafe.Pointer(cpath))
ret = C.SDL_Vulkan_LoadLibrary(cpath)
}
if int(ret) == -1 {
Expand Down

0 comments on commit 7ed18a0

Please sign in to comment.