Skip to content

GMT_IN or GMT_IN|GMT_IS_REFERENCE for external vectors? #3515

Closed

Description

@seisman

The issue was first reported in GenericMappingTools/pygmt#406. The PyGMT script plots two vectors, and PyGMT crashes when plotting the second one.

Here is an equivalent C code, which mimics what PyGMT does.

#include "gmt.h"
int main () {
	void *API = NULL;                 /* The API control structure */
	struct GMT_VECTOR *V = NULL;	  /* Structure to hold input dataset as vectors */
	char input[GMT_VF_LEN] = {""};     			/* String to hold virtual input filename */
	char args[128] = {""};            			/* String to hold module command arguments */

	uint64_t dim[4] = {2, 2, 1, 0};
	double x[2] = {5.0, 5.0};
	double y[2] = {3.0, 8.0};

	/* Initialize the GMT session */
	API = GMT_Create_Session ("test", 2U, GMT_SESSION_EXTERNAL, NULL);
	/* Create a dataset */
	if ((V = GMT_Create_Data (API, GMT_IS_DATASET|GMT_VIA_VECTOR, GMT_IS_POINT, GMT_CONTAINER_ONLY, dim, NULL, NULL, 0, 0, NULL)) == NULL) return (EXIT_FAILURE);
	/**/
	GMT_Put_Vector(API, V, 0, GMT_DOUBLE, x);
	GMT_Put_Vector(API, V, 1, GMT_DOUBLE, y);
	/* Associate our data table with a virtual file */
	//GMT_Open_VirtualFile (API, GMT_IS_DATASET|GMT_VIA_VECTOR, GMT_IS_POINT, GMT_IN|GMT_IS_REFERENCE, V, input);
	GMT_Open_VirtualFile (API, GMT_IS_DATASET|GMT_VIA_VECTOR, GMT_IS_POINT, GMT_IN, V, input);
	/* Prepare the module arguments */
	sprintf (args, "%s -JX10c -R0/10/0/10 -Baf -W1p,black+ve0.2c -P -Vd > vectors.ps", input);
	/* Call the psxy module */
	GMT_Call_Module (API, "psxy", GMT_MODULE_CMD, args);
	GMT_Close_VirtualFile (API, input);
	/* Destroy the GMT session */
	if (GMT_Destroy_Session (API)) return EXIT_FAILURE;
};

The C code crashes at this line

gmt_M_free (GMT, x); gmt_M_free (GMT, y);

However, if I change the parameter GMT_IN to GMT_IN|GMT_IS_REFERENCE when calling GMT_Open_VirtualFile, it works fine.

Is it a bug in function gmtvector_fix_up_path_cartesian? What's the difference between GMT_IN to GMT_IN|GMT_IS_REFERENCE? Should PyGMT always uses GMT_IN|GMT_IS_REFERENCE?

Activity

  1. joa-quim commented on Jun 21, 2020

    @joa-quim
    Member

    As a general rule, all data that is allocated outside GMT must have in the corresponding dataset alloc_mode = GMT_ALLOC_EXTERNALLY so that GMT does not try to free it at the end.

  2. PaulWessel commented on Jun 21, 2020

    @PaulWessel
    Contributor

    GMT_IS_REFERENCE means that GMT must treat what you pass as read-only and cannot modify or free. But I may have a look later (Sunday am is complicated by obligatory morning swim and 2 hour zoom with son) so will get to this in ~5 hours... I am surprised that we look for REFERENCE in the direction argument.

  3. PaulWessel commented on Jun 21, 2020

    @PaulWessel
    Contributor

    Virtual file is created corectly, but when the file is passed to grdinfo and grdinfo calls GMT_Read_Data, the API object has had its family changed from GMT_IS_GRID to GMT_IS_DATASET and it is all downhill from there. Have not found out where family is reset to 0 (GMT_IS_DATASET) so will need to step through very carefully.

  4. seisman commented on Jun 23, 2020

    @seisman
    MemberAuthor

    Ping to draw @PaulWessel's attention to this issue.

  5. PaulWessel commented on Jun 23, 2020

    @PaulWessel
    Contributor

    Surely this was fixed on the weekend. It had to do with not checking direction. Is this still a problem?

  6. seisman commented on Jun 23, 2020

    @seisman
    MemberAuthor

    Do you mean this PR #3514? I don't think it's related to this issue, and it's still failing for me.

  7. PaulWessel commented on Jun 23, 2020

    @PaulWessel
    Contributor

    Well, if it crashes it does not matter what else I fixed. TO debug I need this C code to be in the repo. Is there a branch with this code? Needs to be bild with Xcode to be available to debug.

  8. seisman commented on Jun 23, 2020

    @seisman
    MemberAuthor

    Please work on this PR #3528.

  9. PaulWessel commented on Jun 27, 2020

    @PaulWessel
    Contributor

    We have resolved this issue to use GMT_IN only, unless you are very sure passing the raw pointers will work (GMT_IN|GMT_IS_REFERENCE).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions