CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2013
    Posts
    14

    System Access violation exception:Attempted to read or write protected memory..Help!

    Hello,

    I have CAN Dll program for my application,which was separately used.Now I have included the Drivers program into my application Program and I am having this error System Access Violation Exception:Attempted to read or write protected memory.Please help me what is error for.i am attaching the Dll code and Application code which is throwing this error.

    [CODE]

    The CAN Dll Code

    int receive_data(unsigned char *data_output, int *MsgId, XLportHandle g_xlPortHandle){
    XLstatus xlStatus, xlStatus_new;
    XLevent xlEvent;
    unsigned int msgsrx=RECEIVE_EVENT_SIZE;
    char *local_data ="11111111";
    DWORD status;

    xlStatus_new = xlSetNotification(g_xlPortHandle, &h, 1);

    WaitForSingleObject(h,1);
    xlStatus = XL_SUCCESS;

    while(!xlStatus){
    msgsrx = RECEIVE_EVENT_SIZE;
    xlStatus = xlReceive(g_xlPortHandle, &msgsrx, &xlEvent);---------->Here is the error
    if ( xlStatus!=XL_ERR_QUEUE_IS_EMPTY ) {

    memcpy(data_output,xlEvent.tagData.msg.data,8);
    *MsgId = xlEvent.tagData.msg.id;
    return 0;
    }

    else{
    return XL_ERR_QUEUE_IS_EMPTY;
    }
    }
    }

    My Application Code which is the receiver thread for accessing the messages got onto the CAN bus.

    DWORD WINAPI Rcv_Msg(LPVOID param){
    int *MsgId = new int; //msg id from CAN
    int RcvVal; //0 = there is data in the queue; 1 = there is no data
    unsigned int uMsgId;
    *MsgId = 0;
    unsigned char CanData[8];
    unsigned int i = 0;
    unsigned int j = 0;
    unsigned int Startbit = 0;
    unsigned int Endbit = 0;
    float Offset = 0;
    float Resolution = 0;
    try {
    while(vThread_exit.Rcv_Msg){
    RcvVal = receive_data(CanData, MsgId,g_xlPortHandle_global);-------->Here is the error

    memcpy(&uMsgId,MsgId,4);
    UI64 RawCanData;
    if(RcvVal == 0){ //there is data in the queue
    switch(uMsgId){ //switch statement that parses the incoming message based ON the message id.


    So this program builded successfully and while executing it I am getting the error as

    Receive thread quit Unexpectedly
    system Access violation Exception:Attempted to read or write protected memory.This is often an indication that other memory is corrupt at
    xlreceive(int32,Uint32*,s_xl_event*)
    at receive_data(Byte*data_output,int32*MsgId,Int32 g_xlPortHandle).


    Please help me with this.

    Thanks in Advance.

    [CODE\]

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: System Access violation exception:Attempted to read or write protected memory..He

    Quote Originally Posted by bobby2387 View Post
    Hello,

    I have CAN Dll program for my application,which was separately used.Now I have included the Drivers program into my application Program and I am having this error System Access Violation Exception:Attempted to read or write protected memory.
    Please use proper code tags. The code you posted is unreadable.
    Please help me what is error for.i am attaching the Dll code and Application code which is throwing this error.
    Runtime errors require that we run the program and duplicate the error with the data that you're using. It doesn't matter what the code is, if we don't know what data is being used, the flow of the program, the value of the variables, etc. then it's up to you to debug the program.

    Even the code you posted has unknowns. What is "g_xlPortHandle"? What are the values? Is the code multithreaded? But asking and answering all of these questions still require you to run and debug your application.
    So this program builded successfully and while executing it I am getting the error
    Having a program build successfully means nothing except that an executable was produced. It doesn't mean that the program will run successfully. That is when you must start debugging your code.
    Code:
    		xlStatus = xlReceive(g_xlPortHandle, &msgsrx, &xlEvent);---------->Here is the error
    So if you believe that the error is there, why are you not debugging to see what is going on? What is "xlRecieve"? If you write a program, it is required that you know how to debug the programs that you have written. Otherwise you either gain the proper experience in debugging your own programs, or get someone else to write and debug the programs for you.

    In any event, that line of code shows us nothing. You have "g_xlPortHandle" -- is that a valid value? You are passing the address of msgsrx? Is that a valid address? You are passing the address of &xlEvent. Is that a valid address? If your answer to all three is "yes", then what else can we do for you except to run your program ourselves with the data that you're using? If they are all valid, then the problem is in xlRecieve, or you corrupted memory long before that call is made and that line of code is where your application finally breaks down, or your program is multithreaded and you are not using the proper synchronization or similar problem.

    That's why you need to debug the program, as you have the entire code and application, and can duplicate the error.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured