Segmentation fault in GMT_Destroy_Session (possibly bug)
|Target version:||Candidate for next minor release|
I try to create a grid (Surfer6 binary grid) in the memory and register it as GMT resource (see attached file). Call to GMT_Register_IO returns good resource identifier (not GMT_NOTSET). But next call to GMT_Destroy_Session lead to the segfault. I don't know is this really a bug, because I am not sure, is I calling GMT_Register_IO right. The backtrace is
#0 GMTAPI_close_grd (GMT=GMT@entry=0x7ffff7e47010, G=G@entry=0x7ffff7f78010) at /home/michael/tmp/gmt5-dev/src/gmt_api.c:1394
#1 0x00007ffff78d6016 in GMT_free_grid_ptr (GMT=0x7ffff7e47010, G=G@entry=0x7ffff7f78010, free_grid=free_grid@entry=true) at /home/michael/tmp/gmt5-dev/src/gmt_grdio.c:2133
#2 0x00007ffff78a53b1 in GMTAPI_destroy_data_ptr (API=0x603ea0, family=<optimized out>, ptr=0x7ffff7f78010) at /home/michael/tmp/gmt5-dev/src/gmt_api.c:2410
#3 0x00007ffff78aa110 in GMT_Garbage_Collection (API=0x7ffff7e47010, level=-134774768) at /home/michael/tmp/gmt5-dev/src/gmt_api.c:3943
#4 0x00007ffff78b50d4 in GMT_Destroy_Session (V_API=0x603ea0) at /home/michael/tmp/gmt5-dev/src/gmt_api.c:4087
#5 0x000000000040097f in main ()
Branch 5.2.0, r14972
#1 Updated by Paul almost 3 years ago
- Status changed from New to Resolved
- Assignee set to Paul
You are not passing a GMT_GRID structure but a pointer to some memory. When you say GMT_IS_GRID it means you will be passing a struct GMT_GRID *G as argument. GMT takes you at you word and then tries to do things like freeing allocated resources but gets confused by the bad input.
If you want to create a grid in memory, see GMT_Create_Data for how to do so. Such a grid can then be encoded and passed to modules. grep "GMT_Create_Data" in the gmt source to see examples in the modules.
#2 Updated by Paul almost 3 years ago
- Target version set to Candidate for next minor release
- % Done changed from 0 to 100
We hope to release 5.2 this month around Nov 1, and the API has been strengthened. I've updated the docs on GMT_Register_IO to warn that file pointers many not make sense for certain grid file formats, e.g., netcdf. While GMT proper is quite stable, the API, seeing much less use except for our use in GMT itself, is still considered a beta release. We are grateful for your feedback.
#3 Updated by Michael almost 3 years ago
This is one of the problem of GMT API - void pointers as arguments. Please, consider possibility to use not one function such as GMT_Register_IO, but different functions for each type of argument. Strictly defined types in function calls makes documentation much clearer and API is much safer.
#4 Updated by Paul almost 3 years ago
Tru, of course. But notice the API only accepts a certain list of entities (yes, via void ), but then are well defined and the family argument specifies what is being passed. Clearly, one can add extra security on top of this by having wrapper functions that accept defined types and then can pass the void and family down. But again, this would not work for a surface grid pointer. In that case you will need to consider the GMT_IS_MATRIX option.