0

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. What is error for? I am attaching the Dll code and Application code which is throwing this error.

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;
        }
    }
}

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).
harper
  • 13,345
  • 8
  • 56
  • 105
  • 1
    Perhaps you're supposed to supply the `xlEvent.tagData.msg.data` buffer? Does xlEvent need initialization? – noelicus Mar 18 '13 at 16:36
  • but xlEvent.tagData.msg.data is copied into the buffer data_output. – bobby2387 Mar 18 '13 at 16:51
  • You're not checking for errors on xlSetNotification or WaitForSingleObject. Either or both could be failing, which would likely lead to a crash downstream. Also, I see no declaration for h. What is it and where is it defined? – Carey Gregory Mar 18 '13 at 18:46
  • h is the XLhandle which I have declared as global varible.It is the notification handle for the receive queue. – bobby2387 Mar 18 '13 at 19:08
  • Some structures/classes require a buffer to be set into them, for example `xlEvent.tagData.msg.data = YourBufferThatYouHaveAllocated;` ... it would explain your error, but I don't actually know the xlEvent structure/format/design. – noelicus Mar 18 '13 at 19:47

1 Answers1

0

I had same/similar errors caused by the lack of initialization of the XLevent struct.

That can be achieved with an initialization list:

XLevent xlEvent = {0};

or if you are already using dynamic memory management a memset will do the trick:

XLevent xlEvent;
memset(&xlEvent, 0, sizeof(xlEvent));

These are partially equivalent (the output will be the same). For more info please check this thread: what is the difference between struct {0} and memset 0

Community
  • 1
  • 1
Szendvics
  • 92
  • 3