Thanks, I was wondering about the destructor part since I wouldn't have expected Tsfree to know what to do with the data structures maintained by std::string. I feel more confident using this approach now.
On 1/24/13 9:19 PM, "James Peach" <[email protected]> wrote: >On 24/01/2013, at 3:14 PM, Brian Geffon <[email protected]> wrote: > >> Try just using a placement new. >> >> pData = new ((TransformationData *) >>TSmalloc(sizeof(TransformationData))) TransformationData(); > >and don't forget to call the destructor before calling TSfree: > pData->~TransformationData(); > TSfree(pData); > >> >> Brian >> >> On Thu, Jan 24, 2013 at 2:32 PM, Owens, Steve <[email protected]> >>wrote: >> >> I ran into a bit of an issue today related to TSMalloc >> >> Suppose I have a plugin which defines a plugin data structure such as: >> >> typedef struct { >> int state; >> TSVIO output_vio; >> TSIOBuffer output_buffer; >> TSIOBufferReader output_reader; >> std::string foo; >> } TransformationData; >> >> >> And I have some plugin code which does this: >> >> void allocTransformationData(){ >> TransformationData *pData; >> >> pData = (TransformationData *) TSmalloc(sizeof(TransformationData)); >> pData->state = STATE_START; >> pData->output_buffer = NULL; >> pData->output_vio = NULL; >> pData->output_reader = NULL; >> >> pData->foo.assign("bar"); >> >> return pData; >> } >> >> The line pData->foo.assign("bar"); >> >> >> Will result in a segmentation fault. >> >> Is there a best practice idiom for using std::string items in custom >>continuation data? For example should I be using >> >> typedef struct { >> int state; >> TSVIO output_vio; >> TSIOBuffer output_buffer; >> TSIOBufferReader output_reader; >> std::string *foo; >> } TransformationData; >> >> And >> >> >> Void allocTransformationData(){ >> TransformationData *pData; >> >> pData = (TransformationData *) TSmalloc(sizeof(TransformationData)); >> pData->state = STATE_START; >> pData->output_buffer = NULL; >> pData->output_vio = NULL; >> pData->output_reader = NULL; >> >> >> pData->foo = new std::string; >> pData->foo.assign("bar"); >> >> return pData; >> } >> >> Or should I use: >> >> Void allocTransformationData(){ >> TransformationData *pData; >> >> pData = (TransformationData *) TSmalloc(sizeof(TransformationData)); >> pData->state = STATE_START; >> pData->output_buffer = NULL; >> pData->output_vio = NULL; >> pData->output_reader = NULL; >> >> >> pData->foo = (std::string*) Tsmalloc(sizeof(std::string)); >> pData->foo.assign("bar"); >> >> return pData; >> } >> >> >> >> >> >> >
