From: Bernhard Brodowsky Date: 2012-09-03T08:52:03+09:00 Subject: Re: Common Traps for C extensions Robert Klemme wrote in post #1073887: > On Thu, Aug 30, 2012 at 9:34 AM, Bernhard Brodowsky > wrote: > >> Hi, thanks for your answer. Somehow it didn't compile as C++ code, if I >> do the conversions, > > Well, as long as we do not see the code and / or the error we can't > really tell why it did not compile. I created a test this morning > which worked but threw it away. You do need to compile the catching > and conversion code with extern "C" with a C++ compiler though. > I defined a function with two VALUE args and a VALUE return type and I wanted this to be a method with one argument (plus the self Argument) but I always got this error: error: invalid conversion from 'VALUE (*)(VALUE, VALUE) {aka long unsigned int (*)(long unsigned int, long unsigned int)}' to 'VALUE (*)(...) {aka long unsigned int (*)(...)}' [-fpermissive] >> Exactly, that is what I am talking about, checking for non-null does not >> suffice, because Ruby calls my alloc function first, so the pointer is >> actually valid, but it points to an uninitialized object. > > Yeah, but your allocation function could place one or more NULL > pointers in the structure which get filled later, couldn't it? > Yes, I solved it exactly this way. Henry Maddocks wrote in post #1073985: > On 30/08/2012, at 7:34 PM, Bernhard Brodowsky wrote: > >> >> But now I am thinking of possible traps I might run into. For example, >> the Ruby interpreter forces me to differentiate between allocation and >> initialization and in my current implementation, the User could redefine >> the initialize() method of my class and then call another method, which >> results in undefined behaviour or possibly segfault. > > > Someone might sub-class and not call super. > > If it's possible to seg fault your extension then you are doing it > wrong. After calling the alloc function, your class might not be > 'valid', but it must be 'safe'. This is the reason Ruby added the alloc > hook. Yes, that is what I am trying to achieve, but it seems to be more difficult than I thought in the first moment, but it is safe for any scenario I can think of right now. Cheers, Bernhard -- Posted via http://www.ruby-forum.com/.