Skip to content
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

Buffer leak #164

Merged
merged 3 commits into from
Aug 25, 2017
Merged

Buffer leak #164

merged 3 commits into from
Aug 25, 2017

Conversation

HoneyryderChuck
Copy link
Contributor

ByteBuffer leaks memory, because the char buffer is left behind.

@@ -84,6 +84,7 @@ static void NIO_ByteBuffer_gc_mark(struct NIO_ByteBuffer *buffer)

static void NIO_ByteBuffer_free(struct NIO_ByteBuffer *buffer)
{
xfree(buffer->buffer);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indentation is not aligned. Please use 4 spaces

@tarcieri
Copy link
Contributor

Several of the tests are now segfaulting, e.g.

/home/travis/.rvm/rubies/ruby-2.2.7/lib/ruby/2.2.0/ripper/sexp.rb:111: [BUG] Segmentation fault at 0x00000004d20048
ruby 2.2.7p470 (2017-03-28 revision 58194) [x86_64-linux]

@@ -93,6 +94,7 @@ static VALUE NIO_ByteBuffer_initialize(VALUE self, VALUE capacity)
struct NIO_ByteBuffer *buffer;
Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);

buffer->buffer = NULL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would probably be safer to do this in allocate

@@ -75,6 +75,7 @@ void Init_NIO_ByteBuffer()
static VALUE NIO_ByteBuffer_allocate(VALUE klass)
{
struct NIO_ByteBuffer *bytebuffer = (struct NIO_ByteBuffer *)xmalloc(sizeof(struct NIO_ByteBuffer));
bytebuffer->buffer = NULL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And it might be good to just go ahead and zero the whole structure here, so long as you're changing stuff 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I thought it was going to be an easy patch :) I'll do it tomorrow as I come back to the laptop.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this! This code was from a GSoC project, but it seems I missed this while reviewing it and it really should be properly initialized at the time it's allocated.

Another approach that ensures the buffer is properly allocated would be to malloc a 0 byte buffer, then realloc it once initialize is called. This seems like the safest option to me, although not necessarily the most efficient.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using realloc() in initialize would also ensure if initialize is called repeatedly from Ruby-land no memory would be leaked.

@tarcieri tarcieri merged commit 437885a into socketry:master Aug 25, 2017
@tarcieri
Copy link
Contributor

Ok, well went ahead and merged this because it's an improvement over where it was before.

If you'd like to circle back on the other things I suggested that'd be great, otherwise I might try to tackle them.

@tarcieri
Copy link
Contributor

Hey @UpeksheJay, any interest in fixing up some of the stuff we discussed above?

@HoneyryderChuck
Copy link
Contributor Author

@tarcieri since it's already merged, I'll leave it, seems to be quick chore that can be done before issuing a release.

I assume I'm the first one trying to use byte buffers is an actual project (?). Is the Java version being used in production somewhere?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants