This is the mail archive of the guile@cygnus.com mailing list for the guile project.
Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
---|---|---|
Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |
Hi. I'm using Guile in a C program. I've been having mysterious segfaults recently that seemed to be occuring in the garbage collector. I think that I've finally worked out what the problem is; I had a heap-allocated variable that contained a char* pointer which I wanted to return to Scheme; to do so, I did: return gh_str02scm( data->string ); With this code, my program would periodically segfault. When I changed this line to: return gh_str02scm( strdup( data->string ) ); the problem went away. I assume that this is because Guile doesn't make copies of data you give it but instead stores a pointer which is used for GC-ing; it garbage-collected the data but then I asked for it again, causing a segfualt. I think this should be documented very clearly in the section on converting data between C and Scheme because it isn't obvious which behavior is used, and there are two big pitfalls with the assumption I made: (a) As above..there are cases where you evidently have to either do some fancy tricks to avoid garbage detection or just hand Scheme a copy of the object. (b) Almost worse, I was expecting that the Scheme objects would stay constant if I changed the C variables. I haven't tested this, but I suppose (given that they appear to be the same as the C variables) that they won't. This would probably have broken my code in much more subtle ways if I hadn't been tripped up by the segfault. (conversely, I guess I could find creative ways to make use of this. ;-) ) Basically, I think this issue and similar issues of pointer ownership should be merged into the Guile documentation as soon as someone has time. It would've saved me a couple of hours of frustration at any rate. :-) [ obviously since I still don't fully understand what's going on, I can't really do this.. ] Daniel -- It is hard to think of anything less sentient than a pumpkin. -- Terry Pratchett, _Witches Abroad_