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

Constify Sass_Data_Context::source_string #869

Closed
wants to merge 1 commit into from
Closed

Constify Sass_Data_Context::source_string #869

wants to merge 1 commit into from

Conversation

craigbarnes
Copy link
Contributor

#51, round 2

@craigbarnes
Copy link
Contributor Author

The failed test seems to be caused by an unrelated change in the sass-spec repo.

@xzyfer
Copy link
Contributor

xzyfer commented Feb 2, 2015

Thanks @craigbarnes . You're correct the failure can be ignored for now.

Merge this at your discretion @mgreter

@mgreter
Copy link
Contributor

mgreter commented Feb 3, 2015

It is actually purposely not constant, since the memory you pass as source_string is not copied by libsass; the ownership transfers to libsass, while we do make copies of const chars*. To be honest I'm still confused where to make it const or not, but @QuLogic always gave good advices in this direction!?

@craigbarnes
Copy link
Contributor Author

the ownership transfers to libsass

libsass currently doesn't seem to free source_string. Unless I'm missing something?

On a related note, running sassc via valgrind seems to show several memory leaks in the library.

@QuLogic
Copy link
Contributor

QuLogic commented Feb 4, 2015

libsass currently doesn't seem to free source_string. Unless I'm missing something?

Yes, but line 471 is ctx->source_string = source_string;, i.e., the string is not duplicated. Which means it should continue to exist once the function exits. This is usually signified by leaving off the const (not a hard rule, though.) But you are correct that libsass should probably be freeing it if it owns it.

@craigbarnes
Copy link
Contributor Author

The current contract of the API seems to be:

  • libsass reserves the right to mutate source_string (even though it actually doesn't).
  • libsass leaves it up to the user to guess whether or not to free source_string.

IMHO, libsass should:

  • Promise to not mutate source_string by declaring it const, thereby allowing garbage-collected languages to pass a pointer to the GC'd heap instead of making a full copy.
  • Leave the client responsible for freeing/collecting the memory and consider Sass_Data_Context::source_string a "borrowed" pointer to it.
  • Mention ownership in the API docs, so that knowledge of library internals isn't required for correct usage.

mgreter added a commit to mgreter/libsass that referenced this pull request Mar 7, 2015
Addresses sass#869

This is a breaking change since implementers need to
make a copy of the string if they are not able to pass
the complete memory ownership to libsass.
@mgreter
Copy link
Contributor

mgreter commented Mar 7, 2015

You seem to have a point here, but IMHO we want to go in the other direction.
The one remaining confusion is IMO fixed with this commit: 496f4f9
Unfortunately this is a breaking change for implementers (//CC @am11)

You should always be able to pass libsass the whole memory and libsass will take care to free the memory. We currently keep copies of the pointers in various locations, but finally all these pointers should also be in Context::sources, which will be freed by libsass. So in case your implementation does not allow you to let libsass "overtake" that memory, you have to make a copy yourself before passing it to libsass (the costs should be pretty minimal, what's 1MB of RAM?). We do it that way, because we a) want to allow implementors to only allocate the memory once and pass it to libsass as is and b) we do not trust the outside world with the lifespan of that data (we want to make sure we can keep it around as long as we need, so that libsass is in control of the memory).

I hope this makes sense. If libsass doesn't free the memory, please open a new bug about that. Although my latest memory tests indicate that everything is clear in that direction!

BTW. this was/is also reflected in the C-API:

sass_make_data_context(char* source_string)

@mgreter mgreter closed this Mar 7, 2015
mgreter added a commit to mgreter/libsass that referenced this pull request Mar 7, 2015
Addresses sass#869

This is a breaking change since implementers need to
make a copy of the string if they are not able to pass
the complete memory ownership to libsass.
mgreter added a commit to mgreter/libsass that referenced this pull request Mar 7, 2015
Addresses sass#869

This is a breaking change since implementers need to
make a copy of the string if they are not able to pass
the complete memory ownership to libsass.
mgreter added a commit to mgreter/libsass that referenced this pull request Mar 7, 2015
Addresses sass#869

This is a breaking change since implementers need to
make a copy of the string if they are not able to pass
the complete memory ownership to libsass.
@am11
Copy link
Contributor

am11 commented Mar 7, 2015

Thanks for the heads-up @mgreter! 👍

Is it a good idea that I backport this change to v3.1.0, since node-sass vNext might go without libsass v3.2.0?

This might fix the intermittent hung-ups by libuv and SIGSEGVs which we sometimes encounter with bleeding edge version of io.js.

mgreter added a commit to mgreter/libsass that referenced this pull request Mar 8, 2015
Addresses sass#869

This is a breaking change since implementers need to
make a copy of the string if they are not able to pass
the complete memory ownership to libsass.
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.

5 participants