Skip to content

Commit b2ecbca

Browse files
James Chenminggo
authored andcommitted
fix JSB Memory leaks and crashes (cocos2d#18577)
* [game] Uses global compartment. * fixed cocostudio bug. * Fixes Memory leak for JSB. * Memory leak in ActionsTest.js * js_callfunc binding check whether jsThis is valid. * Fixes potential crash in DownloadDelegator . * CCLOG -> CCLOGINFO for garbage collect callback. * jsb_c_proxy_s is a struct which contains a JS::Heap<> cpp object. Need to use `new`/`delete` to manage its memory. * The functionality of JS controls C++ object's lifecycle isn't stable enough, please don't enable it now. Make an error if user enables it. * Compilation error fixes. * Removes unused JSAutoCompartment since we use JS_EnterCompartment/JS_LeaveCompartment now.
1 parent 8f1459b commit b2ecbca

17 files changed

+321
-445
lines changed

cocos/scripting/js-bindings/auto/js_bindings_chipmunk_auto_classes.cpp

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ bool JSB_cpConstraint_getBodyA(JSContext *cx, uint32_t argc, jsval *vp) {
7676

7777
ret_val = cpConstraintGetBodyA((cpConstraint*)arg0 );
7878

79-
args.rval().set(c_class_to_jsval( cx, ret_val, JS::RootedObject(cx,JSB_cpBody_object), JSB_cpBody_class, "cpBody" ));
79+
JS::RootedObject rootedObj(cx, JSB_cpBody_object);
80+
args.rval().set(c_class_to_jsval( cx, ret_val, rootedObj, JSB_cpBody_class, "cpBody" ));
8081

8182
return true;
8283
}
@@ -94,7 +95,8 @@ bool JSB_cpConstraint_getBodyB(JSContext *cx, uint32_t argc, jsval *vp) {
9495

9596
ret_val = cpConstraintGetBodyB((cpConstraint*)arg0 );
9697

97-
args.rval().set(c_class_to_jsval( cx, ret_val, JS::RootedObject(cx,JSB_cpBody_object), JSB_cpBody_class, "cpBody" ));
98+
JS::RootedObject rootedObj(cx, JSB_cpBody_object);
99+
args.rval().set(c_class_to_jsval( cx, ret_val, rootedObj, JSB_cpBody_class, "cpBody" ));
98100

99101
return true;
100102
}
@@ -202,7 +204,8 @@ bool JSB_cpConstraint_getSpace(JSContext *cx, uint32_t argc, jsval *vp) {
202204

203205
ret_val = cpConstraintGetSpace((cpConstraint*)arg0 );
204206

205-
args.rval().set(c_class_to_jsval( cx, ret_val, JS::RootedObject(cx,JSB_cpSpace_object), JSB_cpSpace_class, "cpSpace" ));
207+
JS::RootedObject rootedObj(cx, JSB_cpSpace_object);
208+
args.rval().set(c_class_to_jsval( cx, ret_val, rootedObj, JSB_cpSpace_class, "cpSpace" ));
206209

207210
return true;
208211
}
@@ -526,7 +529,8 @@ void JSB_cpConstraint_createClass(JSContext *cx, JS::HandleObject globalObj, con
526529
JS_FS_END
527530
};
528531

529-
JSB_cpConstraint_object = JS_InitClass(cx, globalObj, JS::RootedObject(cx,JSB_cpBase_object), JSB_cpConstraint_class, JSB_cpConstraint_constructor,0,properties,funcs,NULL,st_funcs);
532+
JS::RootedObject rootedObj(cx, JSB_cpBase_object);
533+
JSB_cpConstraint_object = JS_InitClass(cx, globalObj, rootedObj, JSB_cpConstraint_class, JSB_cpConstraint_constructor,0,properties,funcs,NULL,st_funcs);
530534
}
531535

532536
/*
@@ -732,7 +736,8 @@ void JSB_cpGrooveJoint_createClass(JSContext *cx, JS::HandleObject globalObj, co
732736
JS_FS_END
733737
};
734738

735-
JSB_cpGrooveJoint_object = JS_InitClass(cx, globalObj, JS::RootedObject(cx,JSB_cpConstraint_object), JSB_cpGrooveJoint_class, JSB_cpGrooveJoint_constructor,0,properties,funcs,NULL,st_funcs);
739+
JS::RootedObject rootedObj(cx, JSB_cpConstraint_object);
740+
JSB_cpGrooveJoint_object = JS_InitClass(cx, globalObj, rootedObj, JSB_cpGrooveJoint_class, JSB_cpGrooveJoint_constructor,0,properties,funcs,NULL,st_funcs);
736741
}
737742

738743
/*
@@ -850,7 +855,8 @@ void JSB_cpSimpleMotor_createClass(JSContext *cx, JS::HandleObject globalObj, co
850855
JS_FS_END
851856
};
852857

853-
JSB_cpSimpleMotor_object = JS_InitClass(cx, globalObj, JS::RootedObject(cx,JSB_cpConstraint_object), JSB_cpSimpleMotor_class, JSB_cpSimpleMotor_constructor,0,properties,funcs,NULL,st_funcs);
858+
JS::RootedObject rootedObj(cx, JSB_cpConstraint_object);
859+
JSB_cpSimpleMotor_object = JS_InitClass(cx, globalObj, rootedObj, JSB_cpSimpleMotor_class, JSB_cpSimpleMotor_constructor,0,properties,funcs,NULL,st_funcs);
854860
}
855861

856862
/*
@@ -1011,7 +1017,8 @@ void JSB_cpPivotJoint_createClass(JSContext *cx, JS::HandleObject globalObj, con
10111017
JS_FS_END
10121018
};
10131019

1014-
JSB_cpPivotJoint_object = JS_InitClass(cx, globalObj, JS::RootedObject(cx,JSB_cpConstraint_object), JSB_cpPivotJoint_class, JSB_cpPivotJoint_constructor,0,properties,funcs,NULL,st_funcs);
1020+
JS::RootedObject rootedObj(cx, JSB_cpConstraint_object);
1021+
JSB_cpPivotJoint_object = JS_InitClass(cx, globalObj, rootedObj, JSB_cpPivotJoint_class, JSB_cpPivotJoint_constructor,0,properties,funcs,NULL,st_funcs);
10151022
}
10161023

10171024
/*
@@ -1216,7 +1223,8 @@ void JSB_cpPinJoint_createClass(JSContext *cx, JS::HandleObject globalObj, const
12161223
JS_FS_END
12171224
};
12181225

1219-
JSB_cpPinJoint_object = JS_InitClass(cx, globalObj, JS::RootedObject(cx,JSB_cpConstraint_object), JSB_cpPinJoint_class, JSB_cpPinJoint_constructor,0,properties,funcs,NULL,st_funcs);
1226+
JS::RootedObject rootedObj(cx, JSB_cpConstraint_object);
1227+
JSB_cpPinJoint_object = JS_InitClass(cx, globalObj, rootedObj, JSB_cpPinJoint_class, JSB_cpPinJoint_constructor,0,properties,funcs,NULL,st_funcs);
12201228
}
12211229

12221230
/*
@@ -1466,7 +1474,8 @@ void JSB_cpSlideJoint_createClass(JSContext *cx, JS::HandleObject globalObj, con
14661474
JS_FS_END
14671475
};
14681476

1469-
JSB_cpSlideJoint_object = JS_InitClass(cx, globalObj, JS::RootedObject(cx,JSB_cpConstraint_object), JSB_cpSlideJoint_class, JSB_cpSlideJoint_constructor,0,properties,funcs,NULL,st_funcs);
1477+
JS::RootedObject rootedObj(cx, JSB_cpConstraint_object);
1478+
JSB_cpSlideJoint_object = JS_InitClass(cx, globalObj, rootedObj, JSB_cpSlideJoint_class, JSB_cpSlideJoint_constructor,0,properties,funcs,NULL,st_funcs);
14701479
}
14711480

14721481
/*
@@ -1628,7 +1637,8 @@ void JSB_cpGearJoint_createClass(JSContext *cx, JS::HandleObject globalObj, cons
16281637
JS_FS_END
16291638
};
16301639

1631-
JSB_cpGearJoint_object = JS_InitClass(cx, globalObj, JS::RootedObject(cx,JSB_cpConstraint_object), JSB_cpGearJoint_class, JSB_cpGearJoint_constructor,0,properties,funcs,NULL,st_funcs);
1640+
JS::RootedObject rootedObj(cx, JSB_cpConstraint_object);
1641+
JSB_cpGearJoint_object = JS_InitClass(cx, globalObj, rootedObj, JSB_cpGearJoint_class, JSB_cpGearJoint_constructor,0,properties,funcs,NULL,st_funcs);
16321642
}
16331643

16341644
/*
@@ -1834,7 +1844,8 @@ void JSB_cpDampedRotarySpring_createClass(JSContext *cx, JS::HandleObject global
18341844
JS_FS_END
18351845
};
18361846

1837-
JSB_cpDampedRotarySpring_object = JS_InitClass(cx, globalObj, JS::RootedObject(cx,JSB_cpConstraint_object), JSB_cpDampedRotarySpring_class, JSB_cpDampedRotarySpring_constructor,0,properties,funcs,NULL,st_funcs);
1847+
JS::RootedObject rootedObj(cx, JSB_cpConstraint_object);
1848+
JSB_cpDampedRotarySpring_object = JS_InitClass(cx, globalObj, rootedObj, JSB_cpDampedRotarySpring_class, JSB_cpDampedRotarySpring_constructor,0,properties,funcs,NULL,st_funcs);
18381849
}
18391850

18401851
/*
@@ -2128,7 +2139,8 @@ void JSB_cpDampedSpring_createClass(JSContext *cx, JS::HandleObject globalObj, c
21282139
JS_FS_END
21292140
};
21302141

2131-
JSB_cpDampedSpring_object = JS_InitClass(cx, globalObj, JS::RootedObject(cx,JSB_cpConstraint_object), JSB_cpDampedSpring_class, JSB_cpDampedSpring_constructor,0,properties,funcs,NULL,st_funcs);
2142+
JS::RootedObject rootedObj(cx, JSB_cpConstraint_object);
2143+
JSB_cpDampedSpring_object = JS_InitClass(cx, globalObj, rootedObj, JSB_cpDampedSpring_class, JSB_cpDampedSpring_constructor,0,properties,funcs,NULL,st_funcs);
21322144
}
21332145

21342146
/*
@@ -2333,7 +2345,8 @@ void JSB_cpRatchetJoint_createClass(JSContext *cx, JS::HandleObject globalObj, c
23332345
JS_FS_END
23342346
};
23352347

2336-
JSB_cpRatchetJoint_object = JS_InitClass(cx, globalObj, JS::RootedObject(cx,JSB_cpConstraint_object), JSB_cpRatchetJoint_class, JSB_cpRatchetJoint_constructor,0,properties,funcs,NULL,st_funcs);
2348+
JS::RootedObject rootedObj(cx, JSB_cpConstraint_object);
2349+
JSB_cpRatchetJoint_object = JS_InitClass(cx, globalObj, rootedObj, JSB_cpRatchetJoint_class, JSB_cpRatchetJoint_constructor,0,properties,funcs,NULL,st_funcs);
23372350
}
23382351

23392352
/*
@@ -2495,7 +2508,8 @@ void JSB_cpRotaryLimitJoint_createClass(JSContext *cx, JS::HandleObject globalOb
24952508
JS_FS_END
24962509
};
24972510

2498-
JSB_cpRotaryLimitJoint_object = JS_InitClass(cx, globalObj, JS::RootedObject(cx,JSB_cpConstraint_object), JSB_cpRotaryLimitJoint_class, JSB_cpRotaryLimitJoint_constructor,0,properties,funcs,NULL,st_funcs);
2511+
JS::RootedObject rootedObj(cx, JSB_cpConstraint_object);
2512+
JSB_cpRotaryLimitJoint_object = JS_InitClass(cx, globalObj, rootedObj, JSB_cpRotaryLimitJoint_class, JSB_cpRotaryLimitJoint_constructor,0,properties,funcs,NULL,st_funcs);
24992513
}
25002514

25012515
/*
@@ -3090,7 +3104,8 @@ void JSB_cpArbiter_createClass(JSContext *cx, JS::HandleObject globalObj, const
30903104
JS_FS_END
30913105
};
30923106

3093-
JSB_cpArbiter_object = JS_InitClass(cx, globalObj, JS::RootedObject(cx,JSB_cpBase_object), JSB_cpArbiter_class, JSB_cpArbiter_constructor,0,properties,funcs,NULL,st_funcs);
3107+
JS::RootedObject rootedObj(cx, JSB_cpBase_object);
3108+
JSB_cpArbiter_object = JS_InitClass(cx, globalObj, rootedObj, JSB_cpArbiter_class, JSB_cpArbiter_constructor,0,properties,funcs,NULL,st_funcs);
30943109
}
30953110

30963111
/*
@@ -3380,7 +3395,8 @@ bool JSB_cpSpace_getStaticBody(JSContext *cx, uint32_t argc, jsval *vp) {
33803395

33813396
ret_val = cpSpaceGetStaticBody((cpSpace*)arg0 );
33823397

3383-
args.rval().set(c_class_to_jsval( cx, ret_val, JS::RootedObject(cx,JSB_cpBody_object), JSB_cpBody_class, "cpBody" ));
3398+
JS::RootedObject rootedObj(cx, JSB_cpBody_object);
3399+
args.rval().set(c_class_to_jsval( cx, ret_val, rootedObj, JSB_cpBody_class, "cpBody" ));
33843400

33853401
return true;
33863402
}
@@ -3398,7 +3414,8 @@ bool JSB_cpSpace_init(JSContext *cx, uint32_t argc, jsval *vp) {
33983414

33993415
ret_val = cpSpaceInit((cpSpace*)arg0 );
34003416

3401-
args.rval().set(c_class_to_jsval( cx, ret_val, JS::RootedObject(cx,JSB_cpSpace_object), JSB_cpSpace_class, "cpSpace" ));
3417+
JS::RootedObject rootedObj(cx, JSB_cpSpace_object);
3418+
args.rval().set(c_class_to_jsval( cx, ret_val, rootedObj, JSB_cpSpace_class, "cpSpace" ));
34023419

34033420
return true;
34043421
}
@@ -3779,7 +3796,8 @@ void JSB_cpSpace_createClass(JSContext *cx, JS::HandleObject globalObj, const ch
37793796
JS_FS_END
37803797
};
37813798

3782-
JSB_cpSpace_object = JS_InitClass(cx, globalObj, JS::RootedObject(cx,JSB_cpBase_object), JSB_cpSpace_class, JSB_cpSpace_constructor,0,properties,funcs,NULL,st_funcs);
3799+
JS::RootedObject rootedObj(cx, JSB_cpBase_object);
3800+
JSB_cpSpace_object = JS_InitClass(cx, globalObj, rootedObj, JSB_cpSpace_class, JSB_cpSpace_constructor,0,properties,funcs,NULL,st_funcs);
37833801
}
37843802

37853803
/*
@@ -4116,7 +4134,8 @@ bool JSB_cpBody_getSpace(JSContext *cx, uint32_t argc, jsval *vp) {
41164134

41174135
ret_val = cpBodyGetSpace((cpBody*)arg0 );
41184136

4119-
args.rval().set(c_class_to_jsval( cx, ret_val, JS::RootedObject(cx,JSB_cpSpace_object), JSB_cpSpace_class, "cpSpace" ));
4137+
JS::RootedObject rootedObj(cx, JSB_cpSpace_object);
4138+
args.rval().set(c_class_to_jsval( cx, ret_val, rootedObj, JSB_cpSpace_class, "cpSpace" ));
41204139

41214140
return true;
41224141
}
@@ -4243,7 +4262,8 @@ bool JSB_cpBody_init(JSContext *cx, uint32_t argc, jsval *vp) {
42434262

42444263
ret_val = cpBodyInit((cpBody*)arg0 , (cpFloat)arg1 , (cpFloat)arg2 );
42454264

4246-
args.rval().set(c_class_to_jsval( cx, ret_val, JS::RootedObject(cx,JSB_cpBody_object), JSB_cpBody_class, "cpBody" ));
4265+
JS::RootedObject rootedObj(cx, JSB_cpBody_object);
4266+
args.rval().set(c_class_to_jsval( cx, ret_val, rootedObj, JSB_cpBody_class, "cpBody" ));
42474267

42484268
return true;
42494269
}
@@ -4717,7 +4737,8 @@ void JSB_cpBody_createClass(JSContext *cx, JS::HandleObject globalObj, const cha
47174737
JS_FS_END
47184738
};
47194739

4720-
JSB_cpBody_object = JS_InitClass(cx, globalObj, JS::RootedObject(cx,JSB_cpBase_object), JSB_cpBody_class, JSB_cpBody_constructor,0,properties,funcs,NULL,st_funcs);
4740+
JS::RootedObject rootedObj(cx, JSB_cpBase_object);
4741+
JSB_cpBody_object = JS_InitClass(cx, globalObj, rootedObj, JSB_cpBody_class, JSB_cpBody_constructor,0,properties,funcs,NULL,st_funcs);
47214742
}
47224743

47234744
/*
@@ -4862,7 +4883,8 @@ bool JSB_cpShape_getBody(JSContext *cx, uint32_t argc, jsval *vp) {
48624883

48634884
ret_val = cpShapeGetBody((cpShape*)arg0 );
48644885

4865-
args.rval().set(c_class_to_jsval( cx, ret_val, JS::RootedObject(cx,JSB_cpBody_object), JSB_cpBody_class, "cpBody" ));
4886+
JS::RootedObject rootedObj(cx, JSB_cpBody_object);
4887+
args.rval().set(c_class_to_jsval( cx, ret_val, rootedObj, JSB_cpBody_class, "cpBody" ));
48664888

48674889
return true;
48684890
}
@@ -5042,7 +5064,8 @@ bool JSB_cpShape_getSpace(JSContext *cx, uint32_t argc, jsval *vp) {
50425064

50435065
ret_val = cpShapeGetSpace((cpShape*)arg0 );
50445066

5045-
args.rval().set(c_class_to_jsval( cx, ret_val, JS::RootedObject(cx,JSB_cpSpace_object), JSB_cpSpace_class, "cpSpace" ));
5067+
JS::RootedObject rootedObj(cx, JSB_cpSpace_object);
5068+
args.rval().set(c_class_to_jsval( cx, ret_val, rootedObj, JSB_cpSpace_class, "cpSpace" ));
50465069

50475070
return true;
50485071
}
@@ -5349,7 +5372,8 @@ void JSB_cpShape_createClass(JSContext *cx, JS::HandleObject globalObj, const ch
53495372
JS_FS_END
53505373
};
53515374

5352-
JSB_cpShape_object = JS_InitClass(cx, globalObj, JS::RootedObject(cx,JSB_cpBase_object), JSB_cpShape_class, JSB_cpShape_constructor,0,properties,funcs,NULL,st_funcs);
5375+
JS::RootedObject rootedObj(cx, JSB_cpBase_object);
5376+
JSB_cpShape_object = JS_InitClass(cx, globalObj, rootedObj, JSB_cpShape_class, JSB_cpShape_constructor,0,properties,funcs,NULL,st_funcs);
53535377
}
53545378

53555379
/*
@@ -5462,7 +5486,8 @@ void JSB_cpCircleShape_createClass(JSContext *cx, JS::HandleObject globalObj, co
54625486
JS_FS_END
54635487
};
54645488

5465-
JSB_cpCircleShape_object = JS_InitClass(cx, globalObj, JS::RootedObject(cx,JSB_cpShape_object), JSB_cpCircleShape_class, JSB_cpCircleShape_constructor,0,properties,funcs,NULL,st_funcs);
5489+
JS::RootedObject rootedObj(cx, JSB_cpShape_object);
5490+
JSB_cpCircleShape_object = JS_InitClass(cx, globalObj, rootedObj, JSB_cpCircleShape_class, JSB_cpCircleShape_constructor,0,properties,funcs,NULL,st_funcs);
54665491
}
54675492

54685493
/*
@@ -5639,7 +5664,8 @@ void JSB_cpSegmentShape_createClass(JSContext *cx, JS::HandleObject globalObj, c
56395664
JS_FS_END
56405665
};
56415666

5642-
JSB_cpSegmentShape_object = JS_InitClass(cx, globalObj, JS::RootedObject(cx,JSB_cpShape_object), JSB_cpSegmentShape_class, JSB_cpSegmentShape_constructor,0,properties,funcs,NULL,st_funcs);
5667+
JS::RootedObject rootedObj(cx, JSB_cpShape_object);
5668+
JSB_cpSegmentShape_object = JS_InitClass(cx, globalObj, rootedObj, JSB_cpSegmentShape_class, JSB_cpSegmentShape_constructor,0,properties,funcs,NULL,st_funcs);
56435669
}
56445670

56455671
/*
@@ -5753,7 +5779,8 @@ void JSB_cpPolyShape_createClass(JSContext *cx, JS::HandleObject globalObj, cons
57535779
JS_FS_END
57545780
};
57555781

5756-
JSB_cpPolyShape_object = JS_InitClass(cx, globalObj, JS::RootedObject(cx,JSB_cpShape_object), JSB_cpPolyShape_class, JSB_cpPolyShape_constructor,0,properties,funcs,NULL,st_funcs);
5782+
JS::RootedObject rootedObj(cx, JSB_cpShape_object);
5783+
JSB_cpPolyShape_object = JS_InitClass(cx, globalObj, rootedObj, JSB_cpPolyShape_class, JSB_cpPolyShape_constructor,0,properties,funcs,NULL,st_funcs);
57575784
}
57585785

57595786

0 commit comments

Comments
 (0)