[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x6d8962]

I have got this error twice and speaking frankly, I am “segmentation violation hater”

Error is :-

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x6d8962]

goroutine 1 [running]:
github.com/aerospike/aerospike-client-go.(*Client).Close(0x0)
        /home/krokite/go/src/github.com/aerospike/aerospike-client-go/client.go:111 +0x22

Would you paste the rest of the stack trace?

That is the only log, I can see in terminal, when I forcefully closed my go app.

It looks like the client object you are calling Close() on is nil!

This is what I have done.

client, err := as.NewClient("127.0.0.1", 3000)
	check(err)
	defer client.Close()

I am really not sure, what went wrong, should I also call function where if app is closed on interrupt should close the connection then?

As I suspected :slight_smile:

If there’s an error, the client object will be nil. You are trying to call a method on a nil pointer.

This is how you should do it:

client, err := as.NewClient("127.0.0.1", 3000)
if err != nil {
    // handle error here
    // probably log and exit the app, or maybe retry
}

// we now know there was no error; so we can use our pointer
defer client.Close()

I strongly recommend reading a bit more about Go from the golang.org before continuing your app. That’s a couple of days of time well spent, and learning the idiomatic way of writing Go programs will pay off in the long run.

Hi, I think, I missed to provide other part.

check(err)   // This is my custom function for checking error

func check(err error) {
    if err != nil {
        fmt.println("Error: "+err.Error())
        panic("gone")
    }
}

That’s how it is handled when i mentioned

client, err := as.NewClient("127.0.0.1", 3000)
check(err)    // <- here
defer client.Close()

then add a nil check around the defer line and see what’s coming in. Are you gettng both a nil error and a nil client?

I just added another close when Interrupted -

client, err := as.NewClient("127.0.0.1", 3000)

// this function will run when program is exited on press of ctrl-c/cmd-c
app.RegisterOnInterrupt(func() {
    // check if client is connected
	if client.IsConnected() {
		client.Close()
	}
})

defer client.Close()

Let me know, if I can use client.IsConnected() function to check and than close on true.?

The problem you are encountering is that your client object is nil.

Multiple close calls to a client should not cause any problem.

You have to find out if and why that client object is nil when there is no error on connect.

how can I find the real culprit, if client is nil, if err would have been nil on first case, it would have immediately thrown an error, but this error comes sometimes when I close the app using ctrl-c in my ubuntu.