问题
I have a class similar to the following. It has a nested enum OptionTag.
class MediaPacket
{
public:
struct RtcpAppName {
char mName[RTCP_APPNAME_LENGTH];
};
enum OptionTag {
RESERVED = 0,
PROFILE = 1,
LATENCY = 2,
PKTLOSS = 3,
JITTER = 4,
LEGACYMIX = 5,
LASTTAG = 255
};
....
....
list<OptionTag> GetOptions(unsigned int ssrc) const;
};
For this I created a swig interface as follows
%module mediaopts_packet
%include "stdint.i"
//Redefining nested structure in swig
struct RtcpAppName {
char mName[RTCP_APPNAME_LENGTH];
};
%{
#define SWIG_FILE_WITH_INIT
#include "../include/mediaopts_packet.h"
%}
//nestedworkaround key for the nested struct
%nestedworkaround CRtcpAppPacket::RtcpAppName;
%{
typedef CRtcpAppPacket::RtcpAppName RtcpAppName;
%}
//Some swig definitions that are not related to this problem
//%include "carrays.i"
//%array_class(uint8_t, buffer);
//%include "typemaps.i"
//%apply (unit8_t *STRING, int LENGTH) { (uint8_t *id, uint32_t len) };
//%apply uint32_t & OUTPUT { uint32_t & xmitOpt };
//%apply uint32_t & OUTPUT { uint32_t & rcvOpt };
//%apply uint32_t & OUTPUT { uint32_t & optValue };
%include "std_list.i"
namespace std
{
%template(ListUint32) list<uint32_t>;
%template(ListOptionTag) list<CRtcpAppMediaoptsPacket::OptionTag>;
}
%include "../include/mediaopts_packet.h"
Using the code in python
Python 2.7.2+ (default, Oct 4 2011, 20:03:08)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mediaopts_packet
>>>
>>> packet = mediaopts_packet.MediaPacket()
>>>
>>> packet.AddSSRC(0,1,1)
>>>
>>> packet.GetAllSSRC()
(0,)
>>> ssrc_list = mediaopts_packet.ListUint32()
>>>
>>> ssrc_list = packet.GetAllSSRC()
>>>
>>> ssrc_list
(0,)
>>> ssrc_list[0]
0
>>> packet.AddOption(0, mediaopts_packet.MediaPacket.RESERVED, 0)
0
>>> packet.GetOptions(0)
Segmentation fault
After building when I use this in python I get a segmentation fault. I am not able to understand what I missed. Please help.
来源:https://stackoverflow.com/questions/10624736/swig-error-with-nested-enums-in-c