Author Topic: Where is the problem? My code? GCC? CodeBlocks?  (Read 8173 times)

Offline cecilio

  • Multiple posting newcomer
  • *
  • Posts: 17
Where is the problem? My code? GCC? CodeBlocks?
« on: January 15, 2013, 07:39:20 pm »
I am debugging some code. Execution has arrived to this method:
Code
void EventNotifier::notify_observers(SpEventInfo pEvent, Observable* target)
{
    std::list<Observer*>::iterator it;
    for (it = m_observers.begin(); it != m_observers.end(); ++it)
    {
        Observable* observedTarget = (*it)->target();
        bool fNotify = (observedTarget == target);
        ...
The last sentence of previous excerpt has been executed. Both variables, observedTarget and target, have the same value but boolean fNotify is false! (please see attached image)

I have no idea of were is the problem. Any help is greately appreciated. Thank you

Cecilio Salmeron
www.lenmus.org

[attachment deleted by admin]

Offline p2rkw

  • Almost regular
  • **
  • Posts: 142
Re: Where is the problem? My code? GCC? CodeBlocks?
« Reply #1 on: January 15, 2013, 07:58:42 pm »
Are you using multiple inheritance?

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Where is the problem? My code? GCC? CodeBlocks?
« Reply #2 on: January 15, 2013, 08:14:39 pm »
I have no idea of were is the problem.
You've asked the question on the wrong forum... see the details in the rules http://forums.codeblocks.org/index.php/topic,9996.0.html
« Last Edit: January 15, 2013, 08:52:59 pm by oBFusCATed »
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline cecilio

  • Multiple posting newcomer
  • *
  • Posts: 17
Re: Where is the problem? My code? GCC? CodeBlocks?
« Reply #3 on: January 15, 2013, 08:24:14 pm »
To p2rkw:

Thank you that's a great clue. I do use inheritance. But both pointers are casted to Observable. How should I do it?


To oBFusCATed
Sorry! When I posted the question I thought it could be something related to Code::Blocks. And the description of the forum is "General (but related to Code::Blocks)
If your post doesn’t fit any of the other topics, post it here :). This is NOT a general programming board.". Sorry again. Please, remove my post if not appropriate. Thank you.




Offline herrtool

  • Single posting newcomer
  • *
  • Posts: 9
Re: Where is the problem? My code? GCC? CodeBlocks?
« Reply #4 on: January 17, 2013, 03:20:53 am »
You're comparing addresses, not values.  You mentioned the values are the same but you didn't mention the addresses.  This is a fairly common mistake (I've made it several times).  :)  Dereference the pointers (assuming Observable has an overloaded == operator).

P.S. - Yes, wrong forum but damage is done.  Hopefully this answer solves your problem (or better yet, you've figured it out already).

Offline cecilio

  • Multiple posting newcomer
  • *
  • Posts: 17
Re: Where is the problem? My code? GCC? CodeBlocks?
« Reply #5 on: January 17, 2013, 05:39:05 pm »
Thank you for answering. I'm still fighting with this issue.

> P.S. - Yes, wrong forum but damage is done.

Sorry again. But I would appreciate a more positive feedback: where should i have posted this question?
Announcements? Not an announcement
Using Code::Blocks? Not a question about the usage of Code::Blocks
Embedded development? Nop
Nightly builds? Nop

So IMHO only two options:

Help [Code::Blocks installation/troubleshooting issues]
General (but related to Code::Blocks)

I thought that Help forum was perhaps more oriented to CB installation issues so I decided (wrongly, I see) to post to General forum. I thought that my problem could be some issue with Code::Blocks debugger.

Really, Which forum should I have used?

Never mind. Sorry for all the damage. Please delete this topic. Thank you.

Offline herrtool

  • Single posting newcomer
  • *
  • Posts: 9
Re: Where is the problem? My code? GCC? CodeBlocks?
« Reply #6 on: January 17, 2013, 05:48:02 pm »
I didn't mean to offend you.  I don't mind that you posted the question.  What I meant was that I was hoping that admins don't mind that I give you a pointer on your problem.

Code
void EventNotifier::notify_observers(SpEventInfo pEvent, Observable* target)
{
    std::list<Observer*>::iterator it;
    for (it = m_observers.begin(); it != m_observers.end(); ++it)
    {
        Observable* observedTarget = (*it)->target();
        bool fNotify = (observedTarget == target);
        ...

replace

Code
bool fNotify = (observedTarget == target);

with

Code
bool fNotify = (*observedTarget == *target);

If you get a compiler error then you need to define a function that's visible to your function above.

Code
bool operator==(const Observable& left, const Observable& right)
{
  return left.foo_member_data_stuff == right.foo_member_data_stuff;
}

Offline p2rkw

  • Almost regular
  • **
  • Posts: 142
Re: Where is the problem? My code? GCC? CodeBlocks?
« Reply #7 on: January 17, 2013, 05:55:04 pm »
@OP: Why you cast these pointers in watch window to lomse::lmoLink* ? Cast them to void* to see their real value. And don't use c style cast while using multiple inheritance. Use static_cast and dynamic_cast instead.

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Where is the problem? My code? GCC? CodeBlocks?
« Reply #8 on: January 17, 2013, 06:52:06 pm »
Topic locked as it violates the forum rules. Nevertheless, enough hints are given anyways...
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ