From: matthew.miller@... (Matthew Miller) Date: 2003-09-29T20:14:57+09:00 Subject: Re: Two problems creating a C++ extension to Ruby Hi Lyle, Lyle Johnson wrote in message news:<3F776A3F.5020300@knology.net>... > Matthew Miller wrote: > > > for the second argument the test fails. I've gotten around this by not doing > > a library test and just calling create_makefile and then editing the > > resulting makefile by hand to have the extension .so linked against > > libid3.so. > > Since libid3.so is not optional in this case, I think it is OK to just > assume that it's there. To avoid having to edit the Makefile, though, > try adding this line to your extconf.rb script before the final call to > create_makefile(): > > $libs = append_library($libs, "id3") That works very well, thanks. It was also necessary for me to change one of the config values: CONFIG['LDSHARED'] = "g++ -shared" > > The second problem came up when I tried to test the extension. After > > creating a new ID3Lib object, which opens an mp3 file, the script calls the > > :artist method to return a string containing the value of that tag. That > > method call generates an ArgumentError exception with the error message > > "NULL pointer given". > > OK. So if we look at the extension code for the ID3Lib#artist method: > > static VALUE id3_get_artist( VALUE obj ) { > ID3Lib* id3 = static_cast(DATA_PTR( obj )); > return rb_str_new2( id3->artist ); > } > > This almost certainly indicates that the id3->artist field is a NULL > pointer. So it is the call to rb_str_new2() that is generating the "NULL > pointer given" error message. > > > I've tried to isolate where that error is generated and I can only say that > > it seems to come from within Ruby. Replacing the body of the C++ function, > > that is called for the no argument version of :artist, with a statement to > > return a Fixnum still results in the "NULL pointer given" exception. So it > > seems that the statements within id3_get_album() don't cause the exception, > > but maybe someother part of my C++ code is the problem? > > The C++ function that implements the ID3Lib#artist method is > id3_get_artist(), not id3_get_album(). You did indeed put some debugging > code in id3_get_album(), but that's not the one that's getting invoked > when you call ID3Lib#artist. Well, these comments got me started in the right direction. When reading the frames of the id3 tag I was using the wrong frame type constant for the artist's name frame. Fixing that and how the char*'s were initialized got everything working. > > If anyone can help, I would be very appreciative. If you look at the code, I > > am also open to comments and suggestions not related to the above problems. > > The code looks really good to me. I suspect that this is just a little > bug somewhere that you'll be able to fix without much trouble. Thanks Lyle, you're right. Once I got a fresh look at the code fixing it wasn't too much trouble. While this extension is still very beta, anyone can get the code here: http://roland.sw.edu/matthew/id3lib_ext-fixed.tar.gz Once I've done more work I'll create a RAA entry... Lyle, again thanks for your help. I believe that nearly everytime I've had a Ruby or FXRuby question you have provided an answer or helped in some way. You have been a great help to me learning Ruby! Take care, Matthew.