View unanswered posts | View active topics It is currently Sat Nov 22, 2014 10:19 am






Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Another Pass-by-reference bug? 
Author Message
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Another Pass-by-reference bug?
Code:
#define MAX_WAIT_EVENTS         4
#define SMF_READY               0x0100
#define SMF_TIMEOUT             0x0200

typedef struct
{
    int  evtType;
    int  evtID;
    int  evtData;
    int  evtParam1;
    int  evtParam2;
    bool fSignaled;
} WAIT_EVT;

typedef struct
{
    int  currState;
    int  nextState;
    long timeout;
    int  smFlags;
    int  nWaitEvents;
    WAIT_EVT WaitEvents[MAX_WAIT_EVENTS];
} SM;

int g_data[2];
SM  g_AutoSM;

void SMClearAllEvents(SM &sm)
{
    nxtDisplayTextLine(1, "ClearEvt:%x", sm.smFlags);
    for (int i = 0; i < MAX_WAIT_EVENTS; ++i)
    {
        sm.WaitEvents[i].evtType = 0;
        sm.WaitEvents[i].evtID = 0;
        sm.WaitEvents[i].evtData = 0;
        sm.WaitEvents[i].evtParam1 = 0;
        sm.WaitEvents[i].evtParam2 = 0;
        sm.WaitEvents[i].fSignaled = false;
    }
    sm.nWaitEvents = 0;
    sm.timeout = 0;
    sm.smFlags &= ~SMF_TIMEOUT;
}   //SMClearAllEvents

void SMInit(SM &sm)
{
    sm.currState = 0;
    sm.nextState = 0;
    sm.smFlags = 0;
}   //SMInit

void SMStart(SM &sm)
{
    if (sm.currState == 0)
    {
        sm.currState = 1;
        sm.nextState = 1;
        sm.smFlags = SMF_READY;
        nxtDisplayTextLine(0, "Before=%x", sm.smFlags);
        SMClearAllEvents(sm);
        nxtDisplayTextLine(2, "After=%x", sm.smFlags);
    }
}   //SMStart

task main()
{
    SMInit(g_AutoSM);
    SMStart(g_AutoSM);
}   //main

If you run the above code, the LCD will give you the following output:
Code:
Before=100
ClearEvt:0
After=0

smFlags was initialized to 0x100 in SMStart(), so "Before=100" is correct. However, when SMClearAllEvents() is called, the smFlags referenced became zero "ClearEvt:0". That's wrong. Then when SMClearAllEvents() returned, the smFlags remained zero "After=0". If I commented out the global variable "int g_data[2]", the output became:
Code:
Before=100
ClearEvt:1
After=100

If I commented out the for-loop or any lines in the for-loop in SMClearAllEvents(), the output became:
Code:
Before=100
ClearEvt:0
After=100

I am using RobotC 3.03, the latest. Can you confirm this is indeed a bug in the compiler? If so, can this be fixed or worked around?

Thanks.


Tue Sep 27, 2011 7:57 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Another Pass-by-reference bug?
After reviewing a similar bug I found last year, I suspect this is the same thing. But Dick told me he had fixed it in ver 2.32. The symptom is very similar to the previous bug report where I passed the structure reference more than one function deep (i.e. the sm structure was passed by reference to the function SMStart which in turn passed it to SMClearAllEvents).
viewtopic.php?f=52&t=2712&hilit=RobotC+bug+concerning+structures


Tue Sep 27, 2011 2:21 pm
Profile
Site Admin
Site Admin

Joined: Wed Jan 24, 2007 10:42 am
Posts: 613
Post Re: Another Pass-by-reference bug?
We're looking into this issue right now. Thanks for reporting!

_________________
Timothy Friez
ROBOTC Developer - SW Engineer
tfriez@robotc.net


Tue Sep 27, 2011 4:41 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Another Pass-by-reference bug?
Any progress on this issue? Is the issue confirmed? If so, is there a fix or work around that I can try?
Thanks.


Thu Sep 29, 2011 11:05 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Another Pass-by-reference bug?
I have been playing with the code and found it is definitely an issue with trying to pass a structure by reference to another function if the structure itself was pass to it by reference. It seems the compiler did not compute the pointer correctly when it is passing it to the second function. This is exactly the same bug I found last year and Dick said it was fixed. If I changed my code to the following, it will fix the issue. Basically, I had to declare a second structure within the first function, copy the structure to the new structure and pass the new structure by reference to the second function. After calling the second function, then copy the new structure content back to the old structure before returning. BTW, am I supposed to be able to copy a structure by assigning the old structure to the new structure (e.g. sm01 = sm02)? If so, that is another bug, structure assignment doesn't work. See the sm01 and sm02 assignment in the code below.
Code:
#define MAX_WAIT_EVENTS         4
#define SMF_READY               0x0100
#define SMF_TIMEOUT             0x0200

typedef struct
{
    int  evtType;
    int  evtID;
    int  evtData;
    int  evtParam1;
    int  evtParam2;
    bool fSignaled;
} WAIT_EVT;

typedef struct
{
    int  currState;
    int  nextState;
    long timeout;
    int  smFlags;
    int  nWaitEvents;
    WAIT_EVT WaitEvents[MAX_WAIT_EVENTS];
} SM;

int g_data[2];
SM  g_AutoSM;
SM  sm01, sm02;

void SMClearAllEvents(SM &sm)
{
    nxtDisplayTextLine(1, "ClearEvt:%x", sm.smFlags);
    for (int i = 0; i < MAX_WAIT_EVENTS; ++i)
    {
        sm.WaitEvents[i].evtType = 0;
        sm.WaitEvents[i].evtID = 0;
        sm.WaitEvents[i].evtData = 0;
        sm.WaitEvents[i].evtParam1 = 0;
        sm.WaitEvents[i].evtParam2 = 0;
        sm.WaitEvents[i].fSignaled = false;
    }
    sm.nWaitEvents = 0;
    sm.timeout = 0;
    sm.smFlags &= ~SMF_TIMEOUT;
}   //SMClearAllEvents

void SMInit(SM &sm)
{
    sm.currState = 0;
    sm.nextState = 0;
    sm.smFlags = 0;
}   //SMInit

void SMStart(SM &sm)
{
    SM sm2;
    if (sm.currState == 0)
    {
        sm.currState = 1;
        sm.nextState = 1;
        sm.smFlags = 0x3FFF;
        nxtDisplayTextLine(0, "Before=%x", sm.smFlags);
        sm2.smFlags = sm.smFlags;
        SMClearAllEvents(sm2);
        sm.smFlags = sm2.smFlags;
        nxtDisplayTextLine(2, "After=%x", sm.smFlags);
    }
}   //SMStart

task main()
{
    sm01.smFlags = 0x1234;
    sm02.smFlags = 0x4321;
    sm01 = sm02;
    nxtDisplayTextLine(4, "1=%x,2=%x", sm01.smFlags, sm02.smFlags);
    SMInit(g_AutoSM);
    SMStart(g_AutoSM);
    nxtDisplayTextLine(3, "D0=%x,D1=%x", g_data[0], g_data[1]);
}   //main


Fri Sep 30, 2011 12:40 am
Profile
Site Admin
Site Admin

Joined: Wed Jan 24, 2007 10:42 am
Posts: 613
Post Re: Another Pass-by-reference bug?
We hope to have the fix available sometime next week. I'm sorry I can't give you any more assistance then that other than we have confirmed the issue and are working on fixing it.

Thanks.

_________________
Timothy Friez
ROBOTC Developer - SW Engineer
tfriez@robotc.net


Fri Sep 30, 2011 2:29 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Another Pass-by-reference bug?
Thanks for the info. In that case, I will wait for the fix and won't try to work around it.


Fri Sep 30, 2011 2:36 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Another Pass-by-reference bug?
Any update on this issue? In particular, is there an ETA on this fix?
Thanks.


Wed Oct 05, 2011 6:47 pm
Profile
Site Admin
Site Admin

Joined: Wed Jan 24, 2007 10:42 am
Posts: 613
Post Re: Another Pass-by-reference bug?
We'll be issuing a fix for this bug (and a few others) by end of day today.

Thanks.

_________________
Timothy Friez
ROBOTC Developer - SW Engineer
tfriez@robotc.net


Mon Oct 10, 2011 9:17 am
Profile
Site Admin
Site Admin

Joined: Wed Jan 24, 2007 10:42 am
Posts: 613
Post Re: Another Pass-by-reference bug?
This is now fixed in 3.04, which is available now.

_________________
Timothy Friez
ROBOTC Developer - SW Engineer
tfriez@robotc.net


Mon Oct 10, 2011 4:16 pm
Profile
Rookie

Joined: Fri Apr 15, 2011 10:29 am
Posts: 37
Post Re: Another Pass-by-reference bug?
tfriez wrote:
This is now fixed in 3.04, which is available now.


Would love to see the release notes for 3.04 if wouldn't mind. :)


Mon Oct 10, 2011 8:50 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Another Pass-by-reference bug?
I second that. Would love to see what's been fixed in 3.04.


Mon Oct 10, 2011 8:59 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3289
Location: Rotterdam, The Netherlands
Post Re: Another Pass-by-reference bug?
Changelog can be found here:

Shameless plug: http://botbench.com/blog/2011/10/11/rob ... available/
(mostly) stolen from here: http://www.robotc.net/blog/?p=1426

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Tue Oct 11, 2011 2:01 am
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Another Pass-by-reference bug?
Is structure assignment supposed to work?
Code:
task main()
{
    sm01.smFlags = 0x1234;
    sm02.smFlags = 0x4321;
    sm01 = sm02;
    nxtDisplayTextLine(1, "sm1=%x,sm2=%x", sm01.smFlags, sm02.smFlags);
}   //main

I tested the above code with the new 3.04 and it still not working.


Tue Oct 11, 2011 3:18 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3289
Location: Rotterdam, The Netherlands
Post Re: Another Pass-by-reference bug?
Do you have a complete program to demonstrate this?

Edit: I tend to use memcpy() for copying structs from one to the other.

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Tue Oct 11, 2011 4:39 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 16 posts ]  Go to page 1, 2  Next

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.