[Qt-interest] A pointer member variable having different values
K. Frank
kfrank29.c at gmail.com
Sun Jan 2 19:39:58 CET 2011
Hello Rohan!
On Sun, Jan 2, 2011 at 9:55 AM, Rohan Prabhu <rohan at rohanprabhu.com> wrote:
> Ok, to begin with, this is my code:
First off, you're really not showing enough code to
get a reasonable answer. I can think of a lot of
reasons consistent with the code you've shown to
get the result you claim
> HyperSprite::HyperSprite()
> {
> _view = 0;
> }
>
> void HyperSprite::publish(QGraphicsView* view) {
> _view = view;
> }
So, calling HyperSprite::publish changes the value of
_view. Why should we assume that publish does not
get called sometime during your test?
> void HyperSprite::getKFrame() {
> if(_view != 0) {
> qDebug()<<(void*)_view;
> }
> }
>
> Now, if I call `HyperSprite::getKFrame()` from within `main()`, I get
> the output:
The way you write this, "HyperSprite::getKFrame()", makes it
sound like you're calling a static member function. . But the rest
of your code fragments _suggest_ that _view is a non-static
member variable, implying that getKFrame is a non-static
member function.
Again, we really can't tell from the limited amount of code you've
shown us.
> 0xbf8ffb84
>
> I have a TCP server, which requires this `QGraphicsView*` variable. So
> whenever a new connection is made, `HyperSprite::getKFrame()` is
> called. However, whenever I make a connection to my server, this is
> the output:
>
> 0x1e425ff
>
> I honestly don't understand this. Shouldn't the value of a member
> remain same throughout? Why is the pointer value changing? As is
> obvious, whenever I try to use the _view pointer to access any of its
> members, a Segmentation Fault occurs. I tried using QSharedPointer,
> but it also results in the same problem. The data of the
> QSharedPointer automatically changes. Why is this happening?
Well, let's assume that the rest of the code you haven't shown us
all consistently implies that _view shouldn't change.
It's still always possible that some other part of your code corrupts
memory. (By the way, we don't even know whether your HyperSprite
lives in the heap or on the stack.) Memory-corruption bugs often
come from buffer overruns, dangling pointers, or use of local variables
after they have left scope.
More information needed.
Good luck.
K. Frank
More information about the Qt-interest-old
mailing list