segfault | Python C extension that segfaults
kandi X-RAY | segfault Summary
kandi X-RAY | segfault Summary
Python C extension that segfaults.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of segfault
segfault Key Features
segfault Examples and Code Snippets
def manager():
"""Returns the multiprocessing manager object for concurrency tools.
The manager object is useful as it controls a server process that holds
the python objects that can be shared across processes. This can be used
for parent-s
Community Discussions
Trending Discussions on segfault
QUESTION
In this simple code:
...ANSWER
Answered 2021-Mar-05 at 14:14You aren't doing p-12 and p-16, you're doing p-3 and p-4.
You are doing (int*)(p-3)
i.e. you are subtracting 3 and then converting to int, not the other way around, ((int*)p)-3
. So the compiler subtracts 3 of whatever type p
is declared to point to, not 3 ints.
That type is void
since p
is a void*
. So the compiler subtracts 3 bytes. Then you read 4 bytes since int
is 4 bytes on your compiler.
Note: subtracting from void*
is a GCC-specific extension - an extra feature that's not part of normal C. Some compilers will give you an error saying you can't add or subtract void*
pointers. On those compilers, to subtract 3 bytes, you would have to cast to char*
and then subtract 3.
QUESTION
I'm working on embedded systems and my goal is to improve the safety of an existing code. I'm trying to follow Nasa's rules : https://en.wikipedia.org/wiki/The_Power_of_10:_Rules_for_Developing_Safety-Critical_Code
The existing code contains dynamically allocated instances and variables which is pretty common, I'm required to translate the program to static memory alocation.
Is there generic practices and patterns to succesfully switch from dynamic to static memory allocation without breaking the code ?
In particular, I'm having issues with those kinds of mallocs :
...ANSWER
Answered 2021-May-10 at 10:09Is there generic practices and patterns to succesfully switch from dynamic to static memory allocation without breaking the code ?
No, not really. You'll have to rewrite all such code in pretty radical ways.
You have to realize why all safety-related and embedded systems ban malloc
. The main reason is that it is non-deterministic. Instead of allowing completely variable sizes, you have to specify a maximum size for each such item, to cover the worst case scenario of the application.
Also, the presence of things like pointer-to-pointers instead of 2D arrays is a pretty certain indication that the original programmer didn't quite know what they were doing in the first place.
Additionally you need to drop the default types of C for stdint.h
ones. That's standard practice in all embedded systems.
In general, I'd strongly advise to drop those "NASA rules" and implement MISRA-C instead. It's a way more professional and in-depth document. Some of the "NASA rules" simply don't make sense and the rest can be summarized as "No s***t Sherlock" beginner-level stuff which we were already told during our first beginner-level C programming class back in school. If these rules come as a surprise to someone, they shouldn't be writing mission-critical firmware in the first place.
QUESTION
Why does my code segfault when using the dynamic memory allocation in the code below ?Any pointers on what maybe happening? , im using valgrind and it points to the second loop being the issue .
...ANSWER
Answered 2021-May-24 at 10:23Syntax? b[y][x] = 0
and not b[i][j] = 0
?
b[y][x]
is out of range.
while b[y-1][x-1]
is the last matrix index.
QUESTION
Problem: I have a class (PortableFoo) designed to be very portable. It contains a scoped class PortableBar. The surrounding codebase (call it Client A) requires both Foo and Bar to have a function that cannot be implemented portably, and Foo's implementation must call Bar's implementation. The following is a solution that compiles and works in GCC, but I know invokes undefined behavior when it casts the reference from base to derived:
...ANSWER
Answered 2021-Jun-11 at 17:37Is it possible to convert Base& to Derived& without object copying or undefined behavior?
Yes, it is possible on a condition that the base reference refers to a base sub object of dynamic type Derived
. A minimal example:
QUESTION
I would like to read a GRIB file downloaded from server using ecCodes library in Rust. However, my current solution results in segmentation fault. The extracted example, replicating the problem, is below.
I download the file using reqwest
crate and get the response as Bytes
1 using bytes()
. To read the file with ecCodes I need to create a codes_handle
using codes_grib_handle_new_from_file()
2, which as argument requires *FILE
usually get from fopen()
. However, I would like to skip IO operations. So I figured I could use libc::fmemopen()
to get *FILE
from Bytes
. But when I pass the *mut FILE
from fmemopen()
to codes_grib_handle_new_from_file()
segmentation fault occurs.
I suspect the issue is when I get from Bytes
a *mut c_void
required by fmemopen()
. I figured I can do this like that:
ANSWER
Answered 2021-Jun-12 at 13:291- Try changing
QUESTION
Other than the usual suspects (process.exit()
, or process termination/signal, or crash/hardware failure), are there any circumstances where code in a finally block will not be reached?
The following typescript code usually executes as expected (using node.js) but occasionally will terminate immediately at line 4 with no exceptions being raised or change in the process exit code (exits 0/success):
...ANSWER
Answered 2021-Mar-12 at 02:03Other than the usual suspects (process.exit(), or process termination/signal, or crash/hardware failure), are there any circumstances where code in a finally block will not be reached?
If the promise will resolve or reject in future then it should reach to the final block.
According to the MDN docs,
The
finally
-block contains statements to execute after thetry
-block andcatch
-block(s) execute, but before the statements following thetry...catch...finally
-block. Note that thefinally
-block executes regardless of whether an exception is thrown. Also, if an exception is thrown, the statements in thefinally
-block execute even if nocatch
-block handles the exception.
A promise is just a JavaScript object. An object can have many states. A promise object can be in pending
state or settled
state. The state settled
can divide as fulfilled
and rejected
. For this example just imagine we have only two state as PENDING
and SETTLED
.
Now if the promise never resolve or reject then it will never go to the settled
state which means your then..catch..finally
will never call. If nothing is reference to the promise then it will just garbage collected.
In your original question you mentioned about a 3rd party async method. If you see that code, the first thing you can see is, there are set of if(..)
blocks to determine the current OS.
But it does not have any else
block or a default case.
What if non of the if(..)
blocks are trigger ? There is nothing to execute and you already returned a promise with return new Promise()
. So basically if non of the if(..)
blocks are triggered, the promise will never change its state from pending
to settled
.
And then as @Bergi also mentioned there are some codes like this. A classic Promise constructor antipattern as he mentioned. For example see the below code,
QUESTION
When I run cpython with the -X showrefcount
flag on an extension I'm writing, it reports a negative reference count (e.g. [-5538 refs, 13503 blocks]
) when I return None from a function (using the Py-RETURN_NONE
macro).
- The exact count varies between runs, but remains within the same order of magnitude.
- Whatever is happening, it seems to happen slowly; I need to call the extension function approximately 50,000 times before the reference count goes negative.
- If we replace
Py_RETURN_NONE;
withPy_INCREF(Py_None); return Py_None;
, it changes nothing. Indeed, we can seemingly add an arbitrary number ofPy_INCREF(Py_None)
s without affecting the reference count at all. - If we replace
Py_RETURN_NONE;
withreturn Py_None;
and don't increment the reference count, it segfaults (as expected). - If we replace the None return with another value, e.g.
PyLong_FromLong(0);
, the problem vanishes.
What is the cause of this? Related question: why is the reference count not zero after running an empty script?
Minimal Example: build command used for cpython debug build ...ANSWER
Answered 2021-Jun-10 at 20:52The problem was due to the extension having been built using an older version of python, and run using a debug build compiled from the latest version of the source. Extensions not compiled using the stable ABI (and declared as doing so) are not binary compatible across python versions.
[Credit to ead's comment for asking the question that led directly to this solution.]
QUESTION
In order not to have to remember to delete, we are using unique_ptr to manage the memory. We were under the impression that we can write and read in the memory, just that deletion is up to the smart pointer. However, the following code crashes on i=7220 with a segfault.
What is wrong?
...ANSWER
Answered 2021-Jun-10 at 19:56unique_ptr mem = make_unique(n);
QUESTION
I am writing a small TCP sockets "library", and I ran into much trouble.
When something happens to the socket that causes it to be instantly closed and freed (regardless of background lingering, talking about user space here), which is only done within a locked mutex since the application I'm writing is multi-threaded, I need a way to tell all the other (potentially) waiting threads (on the same mutex) that want to do something with the socket: "I'm terribly sorry, but the socket has been destroyed and you cannot access it" so that they don't cause any segmentation fault or such.
The idea I had was: the mutex was part of the socket (socket = a structure that contains multiple things, including a mutex and a file descriptor) so I couldn't quite free the socket if other threads were waiting for it (undefined behavior), so the possible solution is to allocate the mutex, free the socket but not the mutex, set some flag (in the allocated memory) saying that the socket has been closed, and a counter so that the last thread waking up and getting notified that it cannot use the socket unlocks and destroys the mutex and frees the memory allocated. The mutex can still be accessed without segfault if we just store its pointer before acquiring the mutex (and the pointer won't ever change).
This solution has a fundamental problem though - what if between unlocking the mutex and freeing it by the last holder, the thread gets preempted and another one locks the mutex again (since you NEED to check socket-related stuff after you acquire the lock, so no way of knowing it got destroyed, unless you maybe use an atomic variable but then again, checking the atomic variable and locking the mutex as a whole are not an atomic operation). Or if you try to access the mutex on the already-freed socket. Undefined behavior emerges.
Any ideas how to solve this problem? I.e. how to destroy a socket so that other threads know about it to quit safely, and there are no race conditions? By quitting I mean aborting the socket function they were in, not cancelling or stopping the threads themselves.
...ANSWER
Answered 2021-Jun-10 at 16:01I could not find a feasible solution of "auto-detecting" when the resources aren't in use (either some kind of a garbage collection, or a timeout since the last call made for that socket, e.g. if the application doesn't do anything with the socket for a minute after it closed, release its resources). That is why I decided to have some reference, look at other things dealing with the problem.
The first very obvious was the kernel itself. If the socket closes, the kernel informs the application of it happening, but only cleans up the socket once the application calls close()
. I think it really is the best way of dealing with this problem, nonetheless it adds some additional responsibility to the application.
QUESTION
I am trying to get setup using GTK3+ and Glade. Unfortunately the most basic setup I can find online is sefaulting. In Glade I just created a basic window with the ID window_main
. I'm not sure how this isn't working.
bytebowl.c
...ANSWER
Answered 2021-Jun-10 at 05:05You need to call gtk_init()
before any other functions from Gtk.
Additionally, gtk_builder_get_object()
does not pass ownership of the widget to the caller, so calling g_object_unref()
on the builder where you do is probably not a good idea.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install segfault
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page