Discussion:
[omniORB] Getting segmentation fault when I delete an array of structs
W T Meyer
2007-06-05 01:18:31 UTC
Permalink
I'm trying to pass an array of fixed-length structs via CORBA. It
almost works, but I get a seqmentation fault at the very end.

Here is the relevant IDL code:
module CoRCC {

enum vmeType {SBC, ROD, TIM, EMPTY, OTHER, UNKNOWNTYPE};
enum vmeModStatus {OK, WARNING, ERROR, UNKNOWNSTATUS};

struct slotInfo {
vmeType contents;
long serNo;
vmeModStatus status;
long revision;
};

typedef slotInfo crateInfo[22];

interface RCC {
long scanCrate(inout crateInfo modList);
};
}

The implementation (server) code seems to work OK; it even returns
the correct information. The problem is on the client side. Here is
the relevant code:

int pyScanCrate(int rccHandle, struct mySlotInfo slotList[]) {
int status = 0;
unsigned int;
int si;
CoRCC::slotInfo* tempList = new CoRCC::slotInfo[24];
status = rccref[rccHandle]->scanCrate((CoRCC::crateInfo_var)tempList);
for (unsigned int i=2; i<NSLOTS+1; i++) {
slotList[i].contents = vmeType((int)tempList[ui].contents);
slotList[i].serNo = tempList[ui].serNo;
slotList[i].status = vmeModStatus((int)tempList[ui].status);
slotList[i].revision = tempList[ui].revision;
}
delete [] tempList;
return status;
}

For technical reasons I need to copy the information into another
array of structs, called slotList, but this should not affect the
CORBA side. I am creating a temporary array of structs, which the
client application owns, and filling it via the call to
rccref[rccHandle]->scanCrate. The information comes back from the
server just fine. I can print it out and everything looks good. The
problem comes on the "delete [] tempList" statement, where I get a
segmentation fault.

If I just create tempList and the delete it without doing anything to
it, there is no problem. It is only after filling tempList from CORBA
that I have a problem.

For the record, I'm running an Intel processor under Linux (Kernel
version 2.4), using g++ 3.2.2, and omniORB 4.0.6.
Luke Deller
2007-06-05 06:15:26 UTC
Permalink
W T Meyer wrote:
...
Post by W T Meyer
CoRCC::slotInfo* tempList = new CoRCC::slotInfo[24];
status = rccref[rccHandle]->scanCrate((CoRCC::crateInfo_var)tempList);
...

The expression "(CoRCC::crateInfo_var)tempList" is not what you want. It will construct a temporary crateInfo_var instance which will claim ownership of tempList. When this temporary instance is destroyed (right after that line of code) then the crateInfo_var destructor will delete tempList.
Post by W T Meyer
delete [] tempList;
This line will delete tempList for the second time, which is why you notice a segmentation fault here.

Try removing the type cast. If you want to use a _var type, then you can declare tempList as that type.

Regards,
Luke.
**********************************************************************************************
Important Note
This email (including any attachments) contains information which is confidential and may be subject to legal privilege. If you are not the intended recipient you must not use, distribute or copy this email. If you have received this email in error please notify the
sender immediately and delete this email. Any views expressed in this email are not necessarily the views of IRESS Market Technology Limited.

It is the duty of the recipient to virus scan and otherwise test the information provided before loading onto any computer system.
IRESS Market Technology Limited does not warrant that the information is free of a virus or any other defect or error.
**********************************************************************************************
W T Meyer
2007-06-05 20:51:51 UTC
Permalink
Removing the type cast indeed fixed the problem. It was a classic
case of making the problem harder than it was. Thanks for the help, Luke.

Regards,

Tom
Post by Luke Deller
...
Post by W T Meyer
CoRCC::slotInfo* tempList = new CoRCC::slotInfo[24];
status = rccref[rccHandle]->scanCrate((CoRCC::crateInfo_var)tempList);
...
The expression "(CoRCC::crateInfo_var)tempList" is not what you
want. It will construct a temporary crateInfo_var instance which
will claim ownership of tempList. When this temporary instance is
destroyed (right after that line of code) then the crateInfo_var
destructor will delete tempList.
Post by W T Meyer
delete [] tempList;
This line will delete tempList for the second time, which is why you
notice a segmentation fault here.
Try removing the type cast. If you want to use a _var type, then
you can declare tempList as that type.
Regards,
Luke.
**********************************************************************************************
Important Note
This email (including any attachments) contains information which is
confidential and may be subject to legal privilege. If you are not
the intended recipient you must not use, distribute or copy this
email. If you have received this email in error please notify the
sender immediately and delete this email. Any views expressed in
this email are not necessarily the views of IRESS Market Technology Limited.
It is the duty of the recipient to virus scan and otherwise test the
information provided before loading onto any computer system.
IRESS Market Technology Limited does not warrant that the
information is free of a virus or any other defect or error.
**********************************************************************************************
Loading...