|
Prev: CComPtr
Next: CComVariant
From: Eric Kaplan on 3 Apr 2008 20:22 why calling PostSoapRequest will crash the program? should I pass string* for start and end? int post_soap_request(string start, string end) { string request = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + start + end; const char* REQUEST_BODY = request.c_str(); ne_set_request_body_buffer(req, REQUEST_BODY, strlen(REQUEST_BODY)); // some more code return 0; } HRESULT PostSoapRequest(string start, string end) { return (post_soap_request(start, end) == 0) ? S_OK : E_FAIL; }
From: Eric Kaplan on 3 Apr 2008 20:55 the problem is parameter using "string start" instead of int post_soap_request(string const& start, string const& end) but i am not very sure the difference string start / string const& start
From: David Lowndes on 4 Apr 2008 02:52 >the problem is parameter using "string start" instead of > >string start / string const& start string start passes the parameter by value (making a copy of it), string &start passes a reference (pointer) to the parameter. They're quite different things, but in your usage may well have the same result! Dave
From: Ulrich Eckhardt on 4 Apr 2008 07:02 Eric Kaplan wrote: > why calling PostSoapRequest will crash the program? "crash" is an interpretation, but it would actually help you get better help if you didn't interpret but told us what you did, what happened and what you expected instead. > int post_soap_request(string start, string end) > { > string request = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > + start + end; > const char* REQUEST_BODY = request.c_str(); Note: you get a pointer to a buffer here which remains valid as long as the 'request' object remains unchanged. Further, it is horrible style to use an ALL_UPPERCASE name for a local variable. Reserve those to macros. > ne_set_request_body_buffer(req, REQUEST_BODY, > strlen(REQUEST_BODY)); If this function only attaches the given pointer to 'req' but doesn't make a copy, you must make sure that it is not used after 'request' above went out of scope. BTW: instead of counting the number of characters in the string, you could have simply used the std::string's length: ne_set_request_body_buffer( req, request.c_str(), request.size()); > HRESULT PostSoapRequest(string start, string end) { > return (post_soap_request(start, end) == 0) ? S_OK : E_FAIL; > } Just a suggestion which will probably be rather invasive at the moment but come handy in the long run: use exceptions. Since composing or copying strings already does that to signal errors, you need to be able to handle them anyway, and it then allows you to concentrate error handling to one place instead of always checking, converting and forwarding returnvalues. Uli -- C++ FAQ: http://parashift.com/c++-faq-lite Sator Laser GmbH Geschäftsführer: Michael Wöhrmann, Amtsgericht Hamburg HR B62 932
|
Pages: 1 Prev: CComPtr Next: CComVariant |