What should I do with a temp np ndarray created in the cython PyObject in __dealloc__












0















I'm trying to change some code and have to create a np.ndarray in a cython class



cdef class T:

def __cinit__(self, X):
# copy a memory space by the numpy way
cdef np.ndarray temp = np.ndarray(shape=X.shape, dtype=X.dtype)
self.X_fake = <DTYPE_t*> temp.data

def __dealloc__(self):
# should I free this?
free(self.X_fake)


I have found nothing in numpy or cython doc. I know if I use malloc to create a DTYPE_t* pointer, it's easy to work, I just wanna know that whether should I free the self.X_fake, or how can I dealloc the temp ndarray, will cython dealloc a numpy class by gc workflow?










share|improve this question























  • In you special case (when it compiles), I wouldn't touch X_fake at all, because it is a dangling pointer as soon as the program is outside of __cinit__.

    – ead
    Nov 22 '18 at 5:23











  • Use ndarray class to create a memory space instead of malloc. Your answer is that the temp will dealloc after cinit, so the pointer will not hold a malloced memory space?

    – Sladar
    Nov 22 '18 at 5:32











  • I wouldn't call it an answer. But yes, this is what will happen. I also would not says that a pointer "holds memory" - it just points to an memory address, which might no longer be accessible and shouldn't be used.

    – ead
    Nov 22 '18 at 5:38











  • Yes I mean that pointer points to the memory block, but after cinit ends, that memory block will be free, which means it would be malloced to another variable.

    – Sladar
    Nov 22 '18 at 6:47


















0















I'm trying to change some code and have to create a np.ndarray in a cython class



cdef class T:

def __cinit__(self, X):
# copy a memory space by the numpy way
cdef np.ndarray temp = np.ndarray(shape=X.shape, dtype=X.dtype)
self.X_fake = <DTYPE_t*> temp.data

def __dealloc__(self):
# should I free this?
free(self.X_fake)


I have found nothing in numpy or cython doc. I know if I use malloc to create a DTYPE_t* pointer, it's easy to work, I just wanna know that whether should I free the self.X_fake, or how can I dealloc the temp ndarray, will cython dealloc a numpy class by gc workflow?










share|improve this question























  • In you special case (when it compiles), I wouldn't touch X_fake at all, because it is a dangling pointer as soon as the program is outside of __cinit__.

    – ead
    Nov 22 '18 at 5:23











  • Use ndarray class to create a memory space instead of malloc. Your answer is that the temp will dealloc after cinit, so the pointer will not hold a malloced memory space?

    – Sladar
    Nov 22 '18 at 5:32











  • I wouldn't call it an answer. But yes, this is what will happen. I also would not says that a pointer "holds memory" - it just points to an memory address, which might no longer be accessible and shouldn't be used.

    – ead
    Nov 22 '18 at 5:38











  • Yes I mean that pointer points to the memory block, but after cinit ends, that memory block will be free, which means it would be malloced to another variable.

    – Sladar
    Nov 22 '18 at 6:47
















0












0








0








I'm trying to change some code and have to create a np.ndarray in a cython class



cdef class T:

def __cinit__(self, X):
# copy a memory space by the numpy way
cdef np.ndarray temp = np.ndarray(shape=X.shape, dtype=X.dtype)
self.X_fake = <DTYPE_t*> temp.data

def __dealloc__(self):
# should I free this?
free(self.X_fake)


I have found nothing in numpy or cython doc. I know if I use malloc to create a DTYPE_t* pointer, it's easy to work, I just wanna know that whether should I free the self.X_fake, or how can I dealloc the temp ndarray, will cython dealloc a numpy class by gc workflow?










share|improve this question














I'm trying to change some code and have to create a np.ndarray in a cython class



cdef class T:

def __cinit__(self, X):
# copy a memory space by the numpy way
cdef np.ndarray temp = np.ndarray(shape=X.shape, dtype=X.dtype)
self.X_fake = <DTYPE_t*> temp.data

def __dealloc__(self):
# should I free this?
free(self.X_fake)


I have found nothing in numpy or cython doc. I know if I use malloc to create a DTYPE_t* pointer, it's easy to work, I just wanna know that whether should I free the self.X_fake, or how can I dealloc the temp ndarray, will cython dealloc a numpy class by gc workflow?







python numpy cython






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 3:57









SladarSladar

409




409













  • In you special case (when it compiles), I wouldn't touch X_fake at all, because it is a dangling pointer as soon as the program is outside of __cinit__.

    – ead
    Nov 22 '18 at 5:23











  • Use ndarray class to create a memory space instead of malloc. Your answer is that the temp will dealloc after cinit, so the pointer will not hold a malloced memory space?

    – Sladar
    Nov 22 '18 at 5:32











  • I wouldn't call it an answer. But yes, this is what will happen. I also would not says that a pointer "holds memory" - it just points to an memory address, which might no longer be accessible and shouldn't be used.

    – ead
    Nov 22 '18 at 5:38











  • Yes I mean that pointer points to the memory block, but after cinit ends, that memory block will be free, which means it would be malloced to another variable.

    – Sladar
    Nov 22 '18 at 6:47





















  • In you special case (when it compiles), I wouldn't touch X_fake at all, because it is a dangling pointer as soon as the program is outside of __cinit__.

    – ead
    Nov 22 '18 at 5:23











  • Use ndarray class to create a memory space instead of malloc. Your answer is that the temp will dealloc after cinit, so the pointer will not hold a malloced memory space?

    – Sladar
    Nov 22 '18 at 5:32











  • I wouldn't call it an answer. But yes, this is what will happen. I also would not says that a pointer "holds memory" - it just points to an memory address, which might no longer be accessible and shouldn't be used.

    – ead
    Nov 22 '18 at 5:38











  • Yes I mean that pointer points to the memory block, but after cinit ends, that memory block will be free, which means it would be malloced to another variable.

    – Sladar
    Nov 22 '18 at 6:47



















In you special case (when it compiles), I wouldn't touch X_fake at all, because it is a dangling pointer as soon as the program is outside of __cinit__.

– ead
Nov 22 '18 at 5:23





In you special case (when it compiles), I wouldn't touch X_fake at all, because it is a dangling pointer as soon as the program is outside of __cinit__.

– ead
Nov 22 '18 at 5:23













Use ndarray class to create a memory space instead of malloc. Your answer is that the temp will dealloc after cinit, so the pointer will not hold a malloced memory space?

– Sladar
Nov 22 '18 at 5:32





Use ndarray class to create a memory space instead of malloc. Your answer is that the temp will dealloc after cinit, so the pointer will not hold a malloced memory space?

– Sladar
Nov 22 '18 at 5:32













I wouldn't call it an answer. But yes, this is what will happen. I also would not says that a pointer "holds memory" - it just points to an memory address, which might no longer be accessible and shouldn't be used.

– ead
Nov 22 '18 at 5:38





I wouldn't call it an answer. But yes, this is what will happen. I also would not says that a pointer "holds memory" - it just points to an memory address, which might no longer be accessible and shouldn't be used.

– ead
Nov 22 '18 at 5:38













Yes I mean that pointer points to the memory block, but after cinit ends, that memory block will be free, which means it would be malloced to another variable.

– Sladar
Nov 22 '18 at 6:47







Yes I mean that pointer points to the memory block, but after cinit ends, that memory block will be free, which means it would be malloced to another variable.

– Sladar
Nov 22 '18 at 6:47














1 Answer
1






active

oldest

votes


















0














As @ead has pointed out, currently temp is an automatic variable that will go out of scope at the end of __cinit__. This means that self.X_fake will be pointing to nothing, and likely cause you problems. Here's the simplest way to fix your code:



import numpy as np
cimport numpy as np

ctypedef np.float64_t DTYPE_t

cdef class T:
cdef np.ndarray temp
cdef DTYPE_t* X_fake

def __cinit__(self, X):
# copy a memory space by the numpy way
self.temp = np.ndarray(shape=X.shape, dtype=X.dtype)
self.X_fake = <DTYPE_t*> self.temp.data


This way, temp will share the lifetime of the T instance to which it belongs. You don't need a __dealloc__. temp will get automatically cleaned up along with everything else in a given instance of T when it goes out of scope/gets garbage collected.



Also, you'll know if you have to free the memory belonging to a pointer, since in that case you'll have created it with malloc in the first place. If you didn't malloc it, don't free it.






share|improve this answer
























  • Thanks for your answer, I know the way to use malloc and free, just don't really know how to deal with ndarray.

    – Sladar
    Nov 22 '18 at 9:48











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423660%2fwhat-should-i-do-with-a-temp-np-ndarray-created-in-the-cython-pyobject-in-deal%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














As @ead has pointed out, currently temp is an automatic variable that will go out of scope at the end of __cinit__. This means that self.X_fake will be pointing to nothing, and likely cause you problems. Here's the simplest way to fix your code:



import numpy as np
cimport numpy as np

ctypedef np.float64_t DTYPE_t

cdef class T:
cdef np.ndarray temp
cdef DTYPE_t* X_fake

def __cinit__(self, X):
# copy a memory space by the numpy way
self.temp = np.ndarray(shape=X.shape, dtype=X.dtype)
self.X_fake = <DTYPE_t*> self.temp.data


This way, temp will share the lifetime of the T instance to which it belongs. You don't need a __dealloc__. temp will get automatically cleaned up along with everything else in a given instance of T when it goes out of scope/gets garbage collected.



Also, you'll know if you have to free the memory belonging to a pointer, since in that case you'll have created it with malloc in the first place. If you didn't malloc it, don't free it.






share|improve this answer
























  • Thanks for your answer, I know the way to use malloc and free, just don't really know how to deal with ndarray.

    – Sladar
    Nov 22 '18 at 9:48
















0














As @ead has pointed out, currently temp is an automatic variable that will go out of scope at the end of __cinit__. This means that self.X_fake will be pointing to nothing, and likely cause you problems. Here's the simplest way to fix your code:



import numpy as np
cimport numpy as np

ctypedef np.float64_t DTYPE_t

cdef class T:
cdef np.ndarray temp
cdef DTYPE_t* X_fake

def __cinit__(self, X):
# copy a memory space by the numpy way
self.temp = np.ndarray(shape=X.shape, dtype=X.dtype)
self.X_fake = <DTYPE_t*> self.temp.data


This way, temp will share the lifetime of the T instance to which it belongs. You don't need a __dealloc__. temp will get automatically cleaned up along with everything else in a given instance of T when it goes out of scope/gets garbage collected.



Also, you'll know if you have to free the memory belonging to a pointer, since in that case you'll have created it with malloc in the first place. If you didn't malloc it, don't free it.






share|improve this answer
























  • Thanks for your answer, I know the way to use malloc and free, just don't really know how to deal with ndarray.

    – Sladar
    Nov 22 '18 at 9:48














0












0








0







As @ead has pointed out, currently temp is an automatic variable that will go out of scope at the end of __cinit__. This means that self.X_fake will be pointing to nothing, and likely cause you problems. Here's the simplest way to fix your code:



import numpy as np
cimport numpy as np

ctypedef np.float64_t DTYPE_t

cdef class T:
cdef np.ndarray temp
cdef DTYPE_t* X_fake

def __cinit__(self, X):
# copy a memory space by the numpy way
self.temp = np.ndarray(shape=X.shape, dtype=X.dtype)
self.X_fake = <DTYPE_t*> self.temp.data


This way, temp will share the lifetime of the T instance to which it belongs. You don't need a __dealloc__. temp will get automatically cleaned up along with everything else in a given instance of T when it goes out of scope/gets garbage collected.



Also, you'll know if you have to free the memory belonging to a pointer, since in that case you'll have created it with malloc in the first place. If you didn't malloc it, don't free it.






share|improve this answer













As @ead has pointed out, currently temp is an automatic variable that will go out of scope at the end of __cinit__. This means that self.X_fake will be pointing to nothing, and likely cause you problems. Here's the simplest way to fix your code:



import numpy as np
cimport numpy as np

ctypedef np.float64_t DTYPE_t

cdef class T:
cdef np.ndarray temp
cdef DTYPE_t* X_fake

def __cinit__(self, X):
# copy a memory space by the numpy way
self.temp = np.ndarray(shape=X.shape, dtype=X.dtype)
self.X_fake = <DTYPE_t*> self.temp.data


This way, temp will share the lifetime of the T instance to which it belongs. You don't need a __dealloc__. temp will get automatically cleaned up along with everything else in a given instance of T when it goes out of scope/gets garbage collected.



Also, you'll know if you have to free the memory belonging to a pointer, since in that case you'll have created it with malloc in the first place. If you didn't malloc it, don't free it.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 9:10









teltel

7,39621431




7,39621431













  • Thanks for your answer, I know the way to use malloc and free, just don't really know how to deal with ndarray.

    – Sladar
    Nov 22 '18 at 9:48



















  • Thanks for your answer, I know the way to use malloc and free, just don't really know how to deal with ndarray.

    – Sladar
    Nov 22 '18 at 9:48

















Thanks for your answer, I know the way to use malloc and free, just don't really know how to deal with ndarray.

– Sladar
Nov 22 '18 at 9:48





Thanks for your answer, I know the way to use malloc and free, just don't really know how to deal with ndarray.

– Sladar
Nov 22 '18 at 9:48




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423660%2fwhat-should-i-do-with-a-temp-np-ndarray-created-in-the-cython-pyobject-in-deal%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]