Fix memleaks in old example

This commit is contained in:
kdeme 2019-11-13 12:04:38 +01:00 committed by zah
parent 8d426de44f
commit 278e230761
1 changed files with 15 additions and 9 deletions

View File

@ -9,6 +9,8 @@ import (
/* /*
#include <stdlib.h>
#cgo LDFLAGS: -Wl,-rpath,'$ORIGIN' -L${SRCDIR}/../build -lnimbus -lm #cgo LDFLAGS: -Wl,-rpath,'$ORIGIN' -L${SRCDIR}/../build -lnimbus -lm
#include "libnimbus.h" #include "libnimbus.h"
@ -39,19 +41,21 @@ func receiveHandler(msg *C.received_message) {
func Start() { func Start() {
C.NimMain() C.NimMain()
fmt.Println("[nim-status] Start Nimbus") fmt.Println("[nim-status] Start Nimbus")
C.nimbus_start(30306, true, false, 0.002, nil, false) if C.nimbus_start(30306, true, false, 0.002, nil, false) == false {
panic("Can't start nimbus")
}
peer1 := "enode://2d3e27d7846564f9b964308038dfadd4076e4373ac938e020708ad8819fd4fd90e5eb8314140768f782db704cb313b60707b968f8b61108a6fecd705b041746d@192.168.0.33:30303" peer1 := C.CString("enode://2d3e27d7846564f9b964308038dfadd4076e4373ac938e020708ad8819fd4fd90e5eb8314140768f782db704cb313b60707b968f8b61108a6fecd705b041746d@192.168.0.33:30303")
peer2 := "enode://4ea35352702027984a13274f241a56a47854a7fd4b3ba674a596cff917d3c825506431cf149f9f2312a293bb7c2b1cca55db742027090916d01529fe0729643b@206.189.243.178:443" C.nimbus_add_peer(peer1)
peer3 := "enode://94d2403d0c55b5c1627eb032c4c6ea8d30b523ae84661aafa18c539ce3af3f114a5bfe1a3cde7776988a6ab2906169dca8ce6a79e32d30c445629b24e6f59e0a@0.0.0.0:30303" C.free(unsafe.Pointer(peer1))
C.nimbus_add_peer(C.CString(peer1))
C.nimbus_add_peer(C.CString(peer2))
C.nimbus_add_peer(C.CString(peer3))
} }
func StatusListenAndPost(channel string) { func StatusListenAndPost(channel string) {
fmt.Println("[nim-status] Status Public ListenAndPost") fmt.Println("[nim-status] Status Public ListenAndPost")
C.nimbus_join_public_chat(C.CString(channel), channelC := C.CString(channel)
defer C.free(unsafe.Pointer(channelC))
C.nimbus_join_public_chat(channelC,
(C.received_msg_handler)(unsafe.Pointer(C.receiveHandler_cgo))) (C.received_msg_handler)(unsafe.Pointer(C.receiveHandler_cgo)))
i := 0 i := 0
for { for {
@ -63,7 +67,9 @@ func StatusListenAndPost(channel string) {
message := fmt.Sprintf("[\"~#c4\",[\"Message:%d\",\"text/plain\",\"~:public-group-user-message\",%d,%d,[\"^ \",\"~:chat-id\",\"%s\",\"~:text\",\"Message:%d\"]]]", i, t*100, t, channel, i) message := fmt.Sprintf("[\"~#c4\",[\"Message:%d\",\"text/plain\",\"~:public-group-user-message\",%d,%d,[\"^ \",\"~:chat-id\",\"%s\",\"~:text\",\"Message:%d\"]]]", i, t*100, t, channel, i)
if i%1000 == 0 { if i%1000 == 0 {
fmt.Println("[nim-status] posting", message) fmt.Println("[nim-status] posting", message)
C.nimbus_post_public(C.CString(channel), C.CString(message)) messageC := C.CString(message)
C.nimbus_post_public(channelC, messageC)
C.free(unsafe.Pointer(messageC))
} }
} }
} }