From a50b000b179a80e0dd7ac28ddcb4fd206d97bd34 Mon Sep 17 00:00:00 2001 From: Wenhai Lin Date: Thu, 21 Apr 2016 23:47:26 +0800 Subject: [PATCH] Fixed memory leak and warns --- native/cocos/2d/CCLabel.cpp | 2 -- native/cocos/2d/CCMenuItem.cpp | 10 +++++----- native/cocos/base/CCEventDispatcher.cpp | 1 - native/cocos/base/CCProperties.cpp | 2 +- .../editor-support/cocosbuilder/CCBReader.cpp | 4 ++-- .../editor-support/cocostudio/CCActionFrame.cpp | 4 ++-- .../editor-support/cocostudio/CCActionFrame.h | 4 ++-- .../cocos/editor-support/cocostudio/CocoLoader.cpp | 6 +++--- .../chipmunk/js_bindings_chipmunk_manual.cpp | 5 ++++- .../js-bindings/manual/js_manual_conversions.cpp | 2 ++ .../js-bindings/manual/network/jsb_websocket.cpp | 14 ++++++++------ native/cocos/ui/UIScale9Sprite.cpp | 11 ++++------- 12 files changed, 33 insertions(+), 32 deletions(-) diff --git a/native/cocos/2d/CCLabel.cpp b/native/cocos/2d/CCLabel.cpp index a29dd089f8..d44a0e7eb1 100644 --- a/native/cocos/2d/CCLabel.cpp +++ b/native/cocos/2d/CCLabel.cpp @@ -857,7 +857,6 @@ bool Label::updateQuads() batchNode->getTextureAtlas()->removeAllQuads(); } - bool letterClamp = false; for (int ctr = 0; ctr < _lengthOfString; ++ctr) { if (_lettersInfo[ctr].valid) @@ -893,7 +892,6 @@ bool Label::updateQuads() _reusedRect.size.width = 0; }else if(_overflow == Overflow::SHRINK){ if (_contentSize.width > letterDef.width) { - letterClamp = true; ret = false; break; }else{ diff --git a/native/cocos/2d/CCMenuItem.cpp b/native/cocos/2d/CCMenuItem.cpp index 8a62642890..819bdb365c 100644 --- a/native/cocos/2d/CCMenuItem.cpp +++ b/native/cocos/2d/CCMenuItem.cpp @@ -383,7 +383,8 @@ void MenuItemSprite::setNormalImage(Node* image) if (image) { addChild(image); - image->setAnchorPoint(Vec2(0, 0)); + image->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT); + setContentSize(image->getContentSize()); } if (_normalImage) @@ -392,7 +393,6 @@ void MenuItemSprite::setNormalImage(Node* image) } _normalImage = image; - this->setContentSize(_normalImage->getContentSize()); this->updateImagesVisibility(); } } @@ -536,7 +536,7 @@ void MenuItemSprite::updateImagesVisibility() { if (_enabled) { - if (_normalImage) _normalImage->setVisible(true); + if (_normalImage) _normalImage->setVisible(true); if (_selectedImage) _selectedImage->setVisible(false); if (_disabledImage) _disabledImage->setVisible(false); } @@ -544,13 +544,13 @@ void MenuItemSprite::updateImagesVisibility() { if (_disabledImage) { - if (_normalImage) _normalImage->setVisible(false); + if (_normalImage) _normalImage->setVisible(false); if (_selectedImage) _selectedImage->setVisible(false); if (_disabledImage) _disabledImage->setVisible(true); } else { - if (_normalImage) _normalImage->setVisible(true); + if (_normalImage) _normalImage->setVisible(true); if (_selectedImage) _selectedImage->setVisible(false); if (_disabledImage) _disabledImage->setVisible(false); } diff --git a/native/cocos/base/CCEventDispatcher.cpp b/native/cocos/base/CCEventDispatcher.cpp index 6bd98ba83f..ca03a133b3 100644 --- a/native/cocos/base/CCEventDispatcher.cpp +++ b/native/cocos/base/CCEventDispatcher.cpp @@ -767,7 +767,6 @@ void EventDispatcher::dispatchEventToListeners(EventListenerVector* listeners, c if (l->isEnabled() && !l->isPaused() && l->isRegistered() && onEvent(l)) { - shouldStopPropagation = true; break; } } diff --git a/native/cocos/base/CCProperties.cpp b/native/cocos/base/CCProperties.cpp index e6acec6349..be483fa3fe 100644 --- a/native/cocos/base/CCProperties.cpp +++ b/native/cocos/base/CCProperties.cpp @@ -140,7 +140,7 @@ static bool isVariable(const char* str, char* outName, size_t outSize) size_t size = len - 3; if (size > (outSize - 1)) size = outSize - 1; - strncpy(outName, str + 2, len - 3); + strncpy(outName, str + 2, size); outName[len - 3] = 0; return true; } diff --git a/native/cocos/editor-support/cocosbuilder/CCBReader.cpp b/native/cocos/editor-support/cocosbuilder/CCBReader.cpp index c07e82e0fc..780bb7ba3e 100644 --- a/native/cocos/editor-support/cocosbuilder/CCBReader.cpp +++ b/native/cocos/editor-support/cocosbuilder/CCBReader.cpp @@ -655,7 +655,7 @@ Node * CCBReader::readNodeGraph(Node * pParent) if(!assigned && this->_CCBMemberVariableAssigner != nullptr) { - assigned = this->_CCBMemberVariableAssigner->onAssignCCBMemberVariable(target, memberVarAssignmentName.c_str(), node); + _CCBMemberVariableAssigner->onAssignCCBMemberVariable(target, memberVarAssignmentName.c_str(), node); } } } @@ -696,7 +696,7 @@ Node * CCBReader::readNodeGraph(Node * pParent) if(!customAssigned && this->_CCBMemberVariableAssigner != nullptr) { - customAssigned = this->_CCBMemberVariableAssigner->onAssignCCBCustomProperty(target, iter->first.c_str(), iter->second); + _CCBMemberVariableAssigner->onAssignCCBCustomProperty(target, iter->first.c_str(), iter->second); } } } diff --git a/native/cocos/editor-support/cocostudio/CCActionFrame.cpp b/native/cocos/editor-support/cocostudio/CCActionFrame.cpp index f98220195c..4937d678ef 100644 --- a/native/cocos/editor-support/cocostudio/CCActionFrame.cpp +++ b/native/cocos/editor-support/cocostudio/CCActionFrame.cpp @@ -229,7 +229,7 @@ ActionMoveFrame::~ActionMoveFrame() { } -void ActionMoveFrame::setPosition(Vec2 pos) +void ActionMoveFrame::setPosition(const Vec2& pos) { _position = pos; } @@ -357,7 +357,7 @@ ActionTintFrame::~ActionTintFrame() } -void ActionTintFrame::setColor(Color3B ccolor) +void ActionTintFrame::setColor(const Color3B& ccolor) { _color = ccolor; } diff --git a/native/cocos/editor-support/cocostudio/CCActionFrame.h b/native/cocos/editor-support/cocostudio/CCActionFrame.h index 711b6aecbc..19fea31557 100644 --- a/native/cocos/editor-support/cocostudio/CCActionFrame.h +++ b/native/cocos/editor-support/cocostudio/CCActionFrame.h @@ -230,7 +230,7 @@ public: * * @param the move action position. */ - void setPosition(cocos2d::Vec2 pos); + void setPosition(const cocos2d::Vec2& pos); /** * Gets the move action position. @@ -429,7 +429,7 @@ public: * * @param ccolor the tint action color */ - void setColor(cocos2d::Color3B ccolor); + void setColor(const cocos2d::Color3B& ccolor); /** * Gets the tint action color. diff --git a/native/cocos/editor-support/cocostudio/CocoLoader.cpp b/native/cocos/editor-support/cocostudio/CocoLoader.cpp index f38b8ca36b..7650e79bcb 100644 --- a/native/cocos/editor-support/cocostudio/CocoLoader.cpp +++ b/native/cocos/editor-support/cocostudio/CocoLoader.cpp @@ -172,16 +172,16 @@ CocoLoader::~CocoLoader() } -bool CocoLoader::ReadCocoBinBuff(char* pBinBuff) +bool CocoLoader::ReadCocoBinBuff(char* pBinBuff) { if(m_pMemoryBuff)return true; char* pTempBuff = pBinBuff; m_pFileHeader = (stCocoFileHeader*)pTempBuff; pTempBuff += sizeof(stCocoFileHeader); - char* pStartAddr = m_pMemoryBuff = pTempBuff; + char* pStartAddr = m_pMemoryBuff = pTempBuff; - char* pDestBuff = new char[m_pFileHeader->m_nDataSize]; + char* pDestBuff = new char[m_pFileHeader->m_nDataSize]; if (m_pFileHeader->m_nCompressSize > 0) { uLongf dwSrcSize = m_pFileHeader->m_nCompressSize; diff --git a/native/cocos/scripting/js-bindings/manual/chipmunk/js_bindings_chipmunk_manual.cpp b/native/cocos/scripting/js-bindings/manual/chipmunk/js_bindings_chipmunk_manual.cpp index 60668efcc3..1a2fa47382 100644 --- a/native/cocos/scripting/js-bindings/manual/chipmunk/js_bindings_chipmunk_manual.cpp +++ b/native/cocos/scripting/js-bindings/manual/chipmunk/js_bindings_chipmunk_manual.cpp @@ -663,7 +663,10 @@ bool jsval_to_array_of_cpvect( JSContext *cx, jsval vp, cpVect**verts, int *numV double value = 0; ok = JS::ToNumber(cx, valarg, &value); - JSB_PRECONDITION( ok, "Error converting value to nsobject"); + if (!ok) { + free(array); + } + JSB_PRECONDITION(ok, "Error converting value to nsobject"); if(i%2==0) array[i/2].x = value; diff --git a/native/cocos/scripting/js-bindings/manual/js_manual_conversions.cpp b/native/cocos/scripting/js-bindings/manual/js_manual_conversions.cpp index 4158c40a4c..05325c24e4 100644 --- a/native/cocos/scripting/js-bindings/manual/js_manual_conversions.cpp +++ b/native/cocos/scripting/js-bindings/manual/js_manual_conversions.cpp @@ -917,6 +917,8 @@ bool jsval_to_ccarray_of_CCPoint(JSContext* cx, JS::HandleValue v, Point **point JS_GetElement(cx, jsobj, i, &valarg); ok = jsval_to_ccpoint(cx, valarg, &array[i]); + if(!ok) + delete [] array; JSB_PRECONDITION3(ok, cx, false, "Error processing arguments"); } diff --git a/native/cocos/scripting/js-bindings/manual/network/jsb_websocket.cpp b/native/cocos/scripting/js-bindings/manual/network/jsb_websocket.cpp index 77d2ace2bb..cb992f8aea 100644 --- a/native/cocos/scripting/js-bindings/manual/network/jsb_websocket.cpp +++ b/native/cocos/scripting/js-bindings/manual/network/jsb_websocket.cpp @@ -292,12 +292,8 @@ bool js_cocos2dx_extension_WebSocket_constructor(JSContext *cx, uint32_t argc, j JS::RootedObject proto(cx, js_cocos2dx_websocket_prototype); JS::RootedObject obj(cx, JS_NewObject(cx, js_cocos2dx_websocket_class, proto, JS::NullPtr())); - //JS::RootedObject obj(cx, JS_NewObjectForConstructor(cx, js_cocos2dx_websocket_class, args)); - - WebSocket* cobj = new (std::nothrow) WebSocket(); - auto delegate = new (std::nothrow) JSB_WebSocketDelegate(cx); - delegate->setJSDelegate(obj); + WebSocket* cobj = nullptr; if (argc == 2) { std::vector protocols; @@ -333,14 +329,20 @@ bool js_cocos2dx_extension_WebSocket_constructor(JSContext *cx, uint32_t argc, j protocols.push_back(protocol); } } + + cobj = new (std::nothrow) WebSocket(); + auto delegate = new (std::nothrow) JSB_WebSocketDelegate(cx); + delegate->setJSDelegate(obj); cobj->init(*delegate, url, &protocols); } else { + cobj = new (std::nothrow) WebSocket(); + auto delegate = new (std::nothrow) JSB_WebSocketDelegate(cx); + delegate->setJSDelegate(obj); cobj->init(*delegate, url); } - JS_DefineProperty(cx, obj, "URL", args.get(0), JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY); //protocol not support yet (always return "") diff --git a/native/cocos/ui/UIScale9Sprite.cpp b/native/cocos/ui/UIScale9Sprite.cpp index aa5c6cddd4..94ee8bf05f 100644 --- a/native/cocos/ui/UIScale9Sprite.cpp +++ b/native/cocos/ui/UIScale9Sprite.cpp @@ -1062,27 +1062,24 @@ namespace ui { bottomHeight = originalSize.height - (capInsets.origin.y + centerHeight) - offsets.w; } - - if(leftWidth<0) + if(leftWidth < 0) { centerWidth += leftWidth; leftWidth = 0; } - if(rightWidth<0) + if(rightWidth < 0) { centerWidth += rightWidth; - rightWidth = 0; } - if(topHeight<0) + if(topHeight < 0) { centerHeight += topHeight; topHeight = 0; } - if(bottomHeight<0) + if(bottomHeight < 0) { centerHeight += bottomHeight; - bottomHeight = 0; } auto textureRect = CC_RECT_POINTS_TO_PIXELS(_spriteRect);