-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PyQGIS QgsPolygon creation crashes #51978
Comments
|
Ownership of rings elements of the list is not transferred. def testPolygonRings(self):
"""Test rings ownership transfer"""
ring = QgsLineString([0.5, 0.5, 0.9, 0.9], [0.5, 0.9, 0.9, 0.5])
p = QgsPolygon(QgsLineString([0, 0, 1, 1], [0, 1, 1, 0]), rings=[ring])
del ring
p.asWkb() # crashes because ring is gone
|
@elpaso , is there a way to work around this? Since this affects also the previous LTS version, I assume I am choosing a less common way to do this? |
You can keep a python reference the individual rings in the array or construct the geometry from WKT.
No idea, sorry.
I didn't check other constructors but there are probably other similar issues in the bindings, I don't really know if this issue has surfaced now due to some changes in the SIP bindings generator or if it has been here forever. I would have expected that when constructing an array of wrapped instances SIP would have increased the ref count but apparently it doesn't. |
Construct the polygon with the exterior ring only, then add interior rings one by one. That should work, since the issue is with lists of rings only. |
I will add another one here, because I have the feeling that it is related. If not, let me know and I will open a different issue. This code makes QGIS crash for me:
PS: again, if someone knows a workaround, it is appreciated. |
there's a bug in your code:
Here you are borrowing a reference to the geometry stored in You could update your code to:
So that you aren't trying to transfer the borrowed point to the collection, but instead are adding a copy of it. I agree that none of this is particularly nice -- it exposes too much of the underlying c++ memory model to Python, and is not expected for regular Python developers. There's no easy way around this for now though -- just be very cautious of any method in the api which is noted as transferring ownership. |
Hi @nyalldawson , thanks for the explanation. Now things are getting clearer. I agree that the exposed C++ memory model is a bit of a problem (and counterintuitive if you come from other languages that allow to keep references of objects in multiple locations). I am trying to create a course for students that are not programmers, so it is ok. But the normal thing that will happen is that they will break things. But in this case it might mean that QGIS crashes. :-/ Anyways, good to know and maybe even the course I am preparing can help to document things a bit. The most important thing is the transferred ownership. Will keep that clear in mind. I have a few questions about the choice of some classes hierarchy. What would be the best place to ask them? The normal QGIS list? The devel list? I didn't see any pyQGIS list, right? |
What is the bug or the crash?
When running the following pyQGIS script in the python editor, QGIS runs properly the first time, but then crashes if the script is re-run.
Steps to reproduce the issue
Versions
<style type="text/css"> p, li { white-space: pre-wrap; } </style>Active Python plugins
sagaprovider
2.12.99
db_manager
0.1.20
processing
2.12.99
grassprovider
2.12.99
MetaSearch
0.3.6
Supported QGIS version
New profile
Additional context
No response
The text was updated successfully, but these errors were encountered: