Click here to Skip to main content
15,896,111 members
Home / Discussions / C#
   

C#

 
QuestionHow to load images on background transperantly? Pin
Supra213-Apr-09 6:56
Supra213-Apr-09 6:56 
AnswerRe: How to load images on background transperantly? Pin
EliottA13-Apr-09 7:09
EliottA13-Apr-09 7:09 
QuestionThreads and Windows Forms [modified] Pin
kikeman13-Apr-09 6:23
kikeman13-Apr-09 6:23 
AnswerRe: Threads and Windows Forms Pin
Dave Kreskowiak13-Apr-09 8:52
mveDave Kreskowiak13-Apr-09 8:52 
GeneralRe: Threads and Windows Forms Pin
kikeman13-Apr-09 10:05
kikeman13-Apr-09 10:05 
GeneralRe: Threads and Windows Forms Pin
Dave Kreskowiak13-Apr-09 10:39
mveDave Kreskowiak13-Apr-09 10:39 
GeneralRe: Threads and Windows Forms Pin
kikeman13-Apr-09 11:10
kikeman13-Apr-09 11:10 
QuestionArray of Structs overwrite problem Pin
Bruce Coward13-Apr-09 5:41
Bruce Coward13-Apr-09 5:41 
I have been chasing this bug for three days now and wondered if anybody out there could point me in the right direction.

I have a circular array generated from a structure. We are writing data to this array and found that the second write was overwriting part of the first record. To simplify debugging we have put some test code that changes what is to be written and then does a second write. Here is the code:

class CAN
{
    // Members
    public struct CANTXSTRUCT
    {
        public int    PGN;
        public byte   DLC;
        public byte[] TxData;
    }

    private const int CANArraySize = 20;
    static private int CANFront = 1;
    static private int CANRear = 0;

    // Create static private circular array of CAN Tx messages
    static private CANTXSTRUCT[] arCANTx = new CANTXSTRUCT[CANArraySize + 1];

    // Methods
    //*********************************************************************
    // static public void QueueCANTxMsg()
    // This method is called to place a CAN Tx message on the rear of the
    // arCANTx circular array.
    //*********************************************************************
    static public void QueueCANTxMsg(int PGN, byte DLC, byte[]TxData)
    {
        // Just warn and return if arCANTx message array is full
        if ((CANRear + 2) % CANArraySize == CANFront)
        {
            MessageBox.Show("CAN Tx Array Overflow, Press OK to continue",
                                    "Error Warning", MessageBoxButtons.OK);
            return;
        }
        // Modulus Increment rear pointer
        CANRear = (CANRear + 1) % CANArraySize;

        // Store data in array
        CAN.arCANTx[CANRear].PGN = PGN;
        CAN.arCANTx[CANRear].DLC = DLC;
        CAN.arCANTx[CANRear].TxData = TxData;

        // Test Code
        TxData[0] = 0xFF;
        CANRear = (CANRear + 1) % CANArraySize;
        // Store test data in array
        CAN.arCANTx[CANRear].PGN = PGN;
        CAN.arCANTx[CANRear].DLC = DLC;
        CAN.arCANTx[CANRear].TxData = TxData;  // <---- Failing line

    } // End of QueueCANTxMsg()


When the failing line is executed it overwrites the first written CAN.arCANTx[CANRear].TxData value. The CANRear value appears to beworking correctly and I think the problem is associated with the byte array copy implicit in the failing line.

Can anyone see what I am doing wrong? Cheers Bruce Confused | :confused:
AnswerRe: Array of Structs overwrite problem Pin
Luc 64801113-Apr-09 6:06
Luc 64801113-Apr-09 6:06 
GeneralRe: Array of Structs overwrite problem Pin
Bruce Coward13-Apr-09 6:24
Bruce Coward13-Apr-09 6:24 
GeneralRe: Array of Structs overwrite problem Pin
Luc 64801113-Apr-09 7:17
Luc 64801113-Apr-09 7:17 
AnswerRe: Array of Structs overwrite problem Pin
Gideon Engelberth13-Apr-09 8:51
Gideon Engelberth13-Apr-09 8:51 
GeneralRe: Array of Structs overwrite problem Pin
Luc 64801113-Apr-09 9:01
Luc 64801113-Apr-09 9:01 
AnswerRe: Array of Structs overwrite problem Pin
Alan N13-Apr-09 9:49
Alan N13-Apr-09 9:49 
GeneralRe: Array of Structs overwrite problem Pin
Luc 64801113-Apr-09 10:29
Luc 64801113-Apr-09 10:29 
QuestionCross-thread error. Pin
Fired.Fish.Gmail13-Apr-09 4:58
Fired.Fish.Gmail13-Apr-09 4:58 
AnswerRe: Cross-thread error. Pin
Colin Angus Mackay13-Apr-09 5:06
Colin Angus Mackay13-Apr-09 5:06 
GeneralRe: Cross-thread error. Pin
Fired.Fish.Gmail13-Apr-09 5:20
Fired.Fish.Gmail13-Apr-09 5:20 
GeneralRe: Cross-thread error. Pin
Henry Minute13-Apr-09 5:22
Henry Minute13-Apr-09 5:22 
AnswerRe: Cross-thread error. Pin
Luc 64801113-Apr-09 5:39
Luc 64801113-Apr-09 5:39 
Questionstruct or class? Pin
Jammer13-Apr-09 4:27
Jammer13-Apr-09 4:27 
AnswerRe: struct or class? Pin
DaveyM6913-Apr-09 4:34
professionalDaveyM6913-Apr-09 4:34 
GeneralRe: struct or class? Pin
Colin Angus Mackay13-Apr-09 4:38
Colin Angus Mackay13-Apr-09 4:38 
AnswerRe: struct or class? Pin
PIEBALDconsult13-Apr-09 5:14
mvePIEBALDconsult13-Apr-09 5:14 
GeneralRe: struct or class? Pin
Colin Angus Mackay13-Apr-09 5:22
Colin Angus Mackay13-Apr-09 5:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.