-1

So I was debugging a program that was showing a Segfault. I ran gdb and from the backtrace I see that the value of (this) changes on calling.

The calling function is of the type

Foo::funcName1() const {
    return Bar::funcName2()->calledFunc();
}  

The called function is of the form

Foo2::calledFunc() const {
    /*
     Some stuff
    */
}

According to the backtrace I have

0x00007ff456a9ec9f in Blah::Meh::funcName1() const (this=0x1f59b40)
0x00007ff44fee181c in Foo2::calledFunc() const (this=0x0)

How can this be happening ?

Adi
  • 89
  • 1
  • 1
  • 13
  • 1
    Since you're having a seg fault, it's probably safe to say that whatever is causing the error is going to mess up many things - including internal pointers – KevinDTimm Sep 29 '15 at 13:29
  • 4
    Could you please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us? The code snippets you show doesn't really tell us anything. All we can guess is that `Bar::funcName2()` apparently returns a null pointer. – Some programmer dude Sep 29 '15 at 13:30
  • Are both the this pointers different ? Thought of it just now. – Adi Sep 29 '15 at 13:31
  • Also, if you have two different objects (no matter the class), then the `this` pointer in each object will be different. The`this` pointer points to the current object, and will be different even in two objects of the same class. – Some programmer dude Sep 29 '15 at 13:32
  • Of course the pointers are different. `Bar::funcName2()` is meant to be called on a `Bar` instance, while `Foo::funcName1()` should be called on a `Foo` instance. Unless one is a subclass of the other (or something like that), they can't have the same address. – Kevin Sep 29 '15 at 13:32
  • It's hard to know if you ask about the `this` pointer, or about the crash. What is it you want help with? – Some programmer dude Sep 29 '15 at 13:35

2 Answers2

1

To answer the Q's title: Your are calling methods on different objects. this points to the current object and hence can change there. Here is an example.

To answer your specific segfault: I'm assuming funcName2 returns a nullptr. You should check that, before invoking calledFunc.

Community
  • 1
  • 1
m8mble
  • 1,513
  • 1
  • 22
  • 30
0

At the risk of sounding redundant...

this points to the object on which you're calling a method. If you have two objects, and you call a method on both of them, they will both have a different this pointer.

In your code, funcName2() returns a NULL pointer. As a result, you're calling a method on a NULL object. Even so, it just so happens that the method returns something, but since you've already dereferenced a NULL pointer, your program's behaviour is now undefined.

You should ensure you don't dereference a NULL pointer. Two options:

  • Make funcName2() not return NULL
  • Make sure that your calling function verifies that the return value of funcName2() is not NULL before trying to do anything with it.
Wouter Verhelst
  • 1,269
  • 1
  • 12
  • 27