Skip to content

Commit ad48261

Browse files
authored
Merge pull request #2400 from mgreter/bugfix/memory-leak
Fix a few minor memory leaks
2 parents debb1e3 + f115eb8 commit ad48261

File tree

8 files changed

+57
-22
lines changed

8 files changed

+57
-22
lines changed

.github/CONTRIBUTING.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
The following is a set of guidelines for contributing to LibSass, which is hosted in the [Sass Organization](https://github.com/sass) on GitHub.
66
These are just guidelines, not rules, use your best judgment and feel free to propose changes to this document in a pull request.
77

8-
LibSass is a library that implements a [sass language] [8] compiler. As such it does not directly interface with end users (frontend developers).
8+
LibSass is a library that implements a [sass language][8] compiler. As such it does not directly interface with end users (frontend developers).
99
For direct contributions to the LibSass code base you will need to have at least a rough idea of C++, we will not lie about that.
1010
But there are other ways to contribute to the progress of LibSass. All contributions are done via github pull requests.
1111

12-
You can also contribute to the LibSass [documentation] [9] or provide additional [spec tests] [10] (and we will gladly point you in the
12+
You can also contribute to the LibSass [documentation][9] or provide additional [spec tests][10] (and we will gladly point you in the
1313
direction for corners that lack test coverage). Foremost we rely on good and concise bug reports for issues the spec tests do not yet catch.
1414

1515
## Precheck: My Sass isn't compiling
16-
- [ ] Check if you can reproduce the issue via [SourceMap Inspector] [5] (updated regularly).
17-
- [ ] Validate official ruby sass compiler via [SassMeister] [6] produces your expected result.
18-
- [ ] Search for similar issue in [LibSass] [1] and [node-sass] [2] (include closed tickets)
19-
- [ ] Optionally test your code directly with [sass] [7] or [sassc] [3] ([installer] [4])
16+
- [ ] Check if you can reproduce the issue via [SourceMap Inspector][5] (updated regularly).
17+
- [ ] Validate official ruby sass compiler via [SassMeister][6] produces your expected result.
18+
- [ ] Search for similar issue in [LibSass][1] and [node-sass][2] (include closed tickets)
19+
- [ ] Optionally test your code directly with [sass][7] or [sassc][3] ([installer][4])
2020

2121
## Precheck: My build/install fails
2222
- [ ] Problems with building or installing libsass should be directed to implementors first!
@@ -47,7 +47,7 @@ direction for corners that lack test coverage). Foremost we rely on good and con
4747
## What makes a code test case
4848

4949
Important is that someone else can get the test case up and running to reproduce it locally. For this
50-
we urge you to verify that your sample yields the expected result by testing it via [SassMeister] [6]
50+
we urge you to verify that your sample yields the expected result by testing it via [SassMeister][6]
5151
or directly via ruby sass or node-sass (or any other libsass implementor) before submitting your bug
5252
report. Once you verified all of the above, you may use the template below to file your bug report.
5353

.github/ISSUE_TEMPLATE.md

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,54 @@
1-
### Title: Be as meaningful as possible in 60 chars if possible
1+
[todo]: # (Title: Be as meaningful as possible)
2+
[todo]: # (Title: Try to use 60 or less chars)
3+
4+
[todo]: # (This is only a template!)
5+
[todo]: # (remove unneeded bits)
6+
[todo]: # (use github preview!)
7+
8+
## input.scss
9+
10+
[todo]: # (always test and report with scss syntax)
11+
[todo]: # (use sass only when results differ from scss)
212

3-
input.scss
413
```scss
514
test {
615
content: bar
716
}
817
```
918

10-
[libsass 3.5.5] [1]
19+
## Actual results
20+
21+
[todo]: # (update version info!)
22+
23+
[libsass 3.X.y][1]
1124
```css
1225
test {
1326
content: bar; }
1427
```
1528

16-
ruby sass 3.4.21
29+
## Expected result
30+
31+
[todo]: # (update version info!)
32+
33+
ruby sass 3.X.y
1734
```css
1835
test {
1936
content: bar; }
2037
```
2138

39+
[todo]: # (update version info!)
40+
[todo]: # (example for node-sass!)
41+
2242
version info:
2343
```cmd
2444
$ node-sass --version
25-
node-sass 3.3.3 (Wrapper) [JavaScript]
26-
libsass 3.2.5 (Sass Compiler) [C/C++]
45+
node-sass 3.X.y (Wrapper) [JavaScript]
46+
libsass 3.X.y (Sass Compiler) [C/C++]
2747
```
2848

49+
[todo]: # (Go to http://libsass.ocbnet.ch/srcmap)
50+
[todo]: # (Enter your SCSS code and hit compile)
51+
[todo]: # (Click `bookmark` and replace the url)
52+
[todo]: # (link is used in actual results above)
53+
2954
[1]: http://libsass.ocbnet.ch/srcmap/#dGVzdCB7CiAgY29udGVudDogYmFyOyB9Cg==

src/context.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ namespace Sass {
6767
plugins(),
6868
emitter(c_options),
6969

70+
ast_gc(),
7071
strings(),
7172
resources(),
7273
sheets(),

src/context.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ namespace Sass {
4343
Plugins plugins;
4444
Output emitter;
4545

46+
// generic ast node garbage container
47+
// used to avoid possible circular refs
48+
std::vector<AST_Node_Obj> ast_gc;
4649
// resources add under our control
4750
// these are guaranteed to be freed
4851
std::vector<char*> strings;

src/expand.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,13 @@ namespace Sass {
176176

177177
Statement_Ptr Expand::operator()(Media_Block_Ptr m)
178178
{
179-
Media_Block_Ptr cpy = m->copy();
179+
Media_Block_Obj cpy = SASS_MEMORY_COPY(m);
180+
// Media_Blocks are prone to have circular references
181+
// Copy could leak memory if it does not get picked up
182+
// Looks like we are able to reset block reference for copy
183+
// Good as it will ensure a low memory overhead for this fix
184+
// So this is a cheap solution with a minimal price
185+
ctx.ast_gc.push_back(cpy); cpy->block(0);
180186
Expression_Obj mq = eval(m->media_queries());
181187
std::string str_mq(mq->to_string(ctx.c_options));
182188
char* str = sass_copy_c_string(str_mq.c_str());

src/memory/SharedPtr.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ namespace Sass {
1717
std::cerr << "###################################\n";
1818
std::cerr << "# REPORTING MISSING DEALLOCATIONS #\n";
1919
std::cerr << "###################################\n";
20-
for (auto var : all) {
21-
if (AST_Node_Ptr ast = Cast<AST_Node>(var)) {
20+
for (SharedObj* var : all) {
21+
if (AST_Node_Ptr ast = dynamic_cast<AST_Node*>(var)) {
2222
debug_ast(ast);
2323
} else {
2424
std::cerr << "LEAKED " << var << "\n";
@@ -60,7 +60,7 @@ namespace Sass {
6060
#endif
6161
if (node->refcounter == 0) {
6262
#ifdef DEBUG_SHARED_PTR
63-
AST_Node_Ptr ptr = Cast<AST_Node>(node);
63+
// AST_Node_Ptr ast = dynamic_cast<AST_Node*>(node);
6464
if (node->dbg) std::cerr << "DELETE NODE " << node << "\n";
6565
#endif
6666
if (!node->detached) {

src/memory/SharedPtr.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Sass {
1717
#ifdef DEBUG_SHARED_PTR
1818

1919
#define SASS_MEMORY_NEW(Class, ...) \
20-
((new Class(__VA_ARGS__))->trace(__FILE__, __LINE__)) \
20+
((Class*)(new Class(__VA_ARGS__))->trace(__FILE__, __LINE__)) \
2121

2222
#define SASS_MEMORY_COPY(obj) \
2323
((obj)->copy(__FILE__, __LINE__)) \

src/parser.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ namespace Sass {
541541
// selector schema re-uses string schema implementation
542542
String_Schema_Ptr schema = SASS_MEMORY_NEW(String_Schema, pstate);
543543
// the selector schema is pretty much just a wrapper for the string schema
544-
Selector_Schema_Ptr selector_schema = SASS_MEMORY_NEW(Selector_Schema, pstate, schema);
544+
Selector_Schema_Obj selector_schema = SASS_MEMORY_NEW(Selector_Schema, pstate, schema);
545545
selector_schema->connect_parent(chroot == false);
546546
selector_schema->media_block(last_media_block);
547547

@@ -605,7 +605,7 @@ namespace Sass {
605605
after_token = before_token = pstate;
606606

607607
// return parsed result
608-
return selector_schema;
608+
return selector_schema.detach();
609609
}
610610
// EO parse_selector_schema
611611

@@ -1668,7 +1668,7 @@ namespace Sass {
16681668
return str_quoted;
16691669
}
16701670

1671-
String_Schema_Ptr schema = SASS_MEMORY_NEW(String_Schema, pstate);
1671+
String_Schema_Obj schema = SASS_MEMORY_NEW(String_Schema, pstate);
16721672
schema->is_interpolant(true);
16731673
while (i < chunk.end) {
16741674
p = constant ? find_first_in_interval< exactly<hash_lbrace> >(i, chunk.end) :
@@ -1704,7 +1704,7 @@ namespace Sass {
17041704
++ i;
17051705
}
17061706

1707-
return schema;
1707+
return schema.detach();
17081708
}
17091709

17101710
String_Constant_Obj Parser::parse_static_value()

0 commit comments

Comments
 (0)