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
fix memory leak in regions(cpu backend) & rgb2ycbcr(api level) #1666
fix memory leak in regions(cpu backend) & rgb2ycbcr(api level) #1666
Conversation
9prady9
commented
Dec 7, 2016
•
edited
edited
- Fixes Memory Leak in CPU regions #1664 . Verified that the leak is not being reported in Valgrind anymore.
- Fixes Memory leak in rgb2ycbcr #1672 . Verified that the leak is not being reported in Valgrind anymore.
@@ -178,6 +178,9 @@ void regions(Array<T> out, const Array<char> in, af_connectivity connectivity) | |||
} | |||
} | |||
|
|||
for (auto& mapElem: lmap) | |||
delete mapElem.second; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better if you stored unique_ptrs in the lmap so you don't have to do this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about unique pointers, but then i thought why add another abstraction instead of just cleaning them off at the end since this object's definition and usage is local to that function. Will using unique pointers make it more efficient ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unique ptrs aren't abstractions. They are there to make this type of thing from not happening again. You should almost never use raw pointers
130ea04
to
fb645bb
Compare
@@ -103,7 +104,8 @@ void regions(Array<T> out, const Array<char> in, af_connectivity connectivity) | |||
T *out_ptr = out.get(); | |||
|
|||
// Map labels | |||
typedef typename std::map<T, LabelNode<T>* > label_map_t; | |||
typedef typename std::unique_ptr< LabelNode<T> > UnqLabelPtr; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use consistent naming conventions within a file.
// Insert new label in map | ||
LabelNode<T> *node = new LabelNode<T>(label); | ||
lmap.insert(std::pair<T, LabelNode<T>* >(label, node)); | ||
lmap.insert(std::pair<T, UnqLabelPtr>(label, UnqLabelPtr(new LabelNode<T>(label)))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use make_pair instead of the pair constructor.
fb645bb
to
f8590d4
Compare
build arrayfire windows cpu ci |
Please look at the updated changes.
indices[2] = {1, 1, 1}; | ||
Array<T> Y = createSubArray(input, indices, false); | ||
|
||
indices[2] = {2, 2, 1}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you are making another commit in this PR, can you add a comment saying why you are only assigning to indices[2]
in all the 3 cases. I think it would help code readability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shehzan10 Because he is reading the three channels into 3 different arrays ?
@9prady9 Can you check if something like createSubArray(input, {af_span, af_span, {1,1,1}}, false);
works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have left comments before the beginning of extraction of channels. In any case, i don't have any other commits left in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know why he is doing it, but it took me a bit to realize it. That's why I said "if you are making another commit", because its not that much of a priority. It just helps anyone in the future who is going to read it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pavanky I checked by running the existing tests, why would we need copy's of original Array ? Just indexed Array's are sufficient right ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a possibility that the meta-data may not be properly altered ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@9prady9 Check my comment again. I did not say anything about copy, I was wondering if array initializers work instead of using vectors.
That is the reason I asked for clarification. I didn't get what you were
concerned about. They seem to be working.
…On Thu, Dec 15, 2016, 10:06 PM Pavan Yalamanchili ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In src/api/c/ycbcr_rgb.cpp
<#1666>:
> // get Array objects for corresponding channel views
- Array<T> X = getArray<T>(ch1Temp);
- Array<T> Y = getArray<T>(ch2Temp);
- Array<T> Z = getArray<T>(ch3Temp);
+ const Array<T>& input = getArray<T>(in);
+ std::vector<af_seq> indices(4, af_span);
+
+ indices[2] = {0, 0, 1};
+ Array<T> X = createSubArray(input, indices, false);
+
+ indices[2] = {1, 1, 1};
+ Array<T> Y = createSubArray(input, indices, false);
+
+ indices[2] = {2, 2, 1};
@9prady9 <https://github.com/9prady9> Check my comment again. I did not
say anything about copy, I was wondering if array initializers work instead
of using vectors.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1666>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/ADHnOqjvueNe8FikW7IyIzJrBozPoeOyks5rIWx0gaJpZM4LGiAw>
.
|
typedef typename std::map<T, LabelNode<T>* > label_map_t; | ||
typedef typename label_map_t::iterator label_map_iterator_t; | ||
typedef typename std::unique_ptr< LabelNode<T> > UnqLabelPtr; | ||
typedef typename std::map<T, UnqLabelPtr > LabelMap; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is only used once. It is an unnecessary indirection but not a big deal. Your call
Also. Use the using keyword for type aliasing. They are easier to read and more powerful.
using LabelMap = typename std::map<T, UnqLabelPtr>;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just one character smaller..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's easier (for me) to read because it uses the assignment style to define the alias, not because it is shorter.
for (int j = 0; j < (int)inDims[1]; j++) { | ||
for (int i = 0; i < (int)inDims[0]; i++) { | ||
int idx = j * inDims[0] + i; | ||
if (inPtr[idx] != 0) { | ||
std::vector<T> l; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better if this was a std::array instead of a std::vector. Since std::arrays are allocated on the stack, you don't get the penalty of calling malloc.
I know its not part of this PR. Perhaps in another issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good.