fix textview's memleak,remove StateSet::VIEW_STATE_SETS and StateSet::createStates

This commit is contained in:
houzh 2022-03-15 10:31:34 +00:00
parent 19c2d3aad3
commit 210d02acfd
10 changed files with 31 additions and 43 deletions

View File

@ -86,6 +86,9 @@ void WindowManager::removeWindow(Window*w){
w1->invalidate(&rc);
}
Looper::getDefault()->removeEventHandler(w->source);
View::AttachInfo*info=w->mAttachInfo;
w->dispatchDetachedFromWindow();
delete info;
delete w;
for(auto it=windows.rbegin();it!=windows.rend();it++){
if((*it)->hasFlag(View::FOCUSABLE)&&(*it)->getVisibility()==View::VISIBLE){

View File

@ -19,42 +19,17 @@ std::vector<int>StateSet::VIEW_STATE_IDS={
DRAG_HOVERED , VIEW_STATE_DRAG_HOVERED
};
std::vector<std::vector<int>>StateSet::VIEW_STATE_SETS;
void StateSet::trimStateSet(std::vector<int>&states,int newsize){
states.resize(newsize);
}
void StateSet::createStates(){
const int NUM_BITS=VIEW_STATE_IDS.size()/2;
std::vector<int>orderedIds;
orderedIds.resize(VIEW_STATE_IDS.size());
for (int i = 0; i < NUM_BITS;i++){//R.styleable.ViewDrawableStates.length; i++) {
int viewState = i+1;//R.styleable.ViewDrawableStates[i];STATE is codedfrom WINDOW_FOCUSED:1-->DRAG_HOVERED:10
for (int j = 0; j < VIEW_STATE_IDS.size(); j += 2) {
if (VIEW_STATE_IDS[j] == viewState) {
orderedIds[i * 2] = viewState;
orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
}
}
std::vector<int> StateSet::get(int mask){
std::vector<int>states;
for( int i=0 ; i<VIEW_STATE_IDS.size() ; i+=2 ){
if( mask & VIEW_STATE_IDS[i+1] )
states.push_back(VIEW_STATE_IDS[i]);
}
VIEW_STATE_SETS.resize(1<<NUM_BITS);
for (int i = 0; i < VIEW_STATE_SETS.size(); i++) {
std::vector<int>set;
for (int j = 0; j < orderedIds.size(); j += 2) {
if ((i & orderedIds[j + 1]) != 0) {
set.push_back(orderedIds[j]);
}
}
VIEW_STATE_SETS[i] = set;
}
}
std::vector<int>& StateSet::get(int mask){
if(VIEW_STATE_SETS.size()==0)
createStates();
return VIEW_STATE_SETS[mask];
return states;
}
bool StateSet::isWildCard(const std::vector<int>&stateSetOrSpec){

View File

@ -1,15 +1,14 @@
#ifndef __STATESET_H__
#define __STATESET_H__
#include <string>
#include <vector>
#include <core/attributeset.h>
namespace cdroid{
class StateSet{
private:
static std::vector<int>VIEW_STATE_IDS;
static std::vector<std::vector<int>>VIEW_STATE_SETS;
static void appendState(std::vector<int>&states,const std::string&s,int value);
static void createStates();
public:
enum{
VIEW_STATE_WINDOW_FOCUSED =1<<0 ,
@ -46,7 +45,7 @@ public:
static bool stateSetMatches(const std::vector<int>&stateSpec,int state);
static bool containsAttribute(const std::vector<std::vector<int>>&stateSpecs,int attr);
static int parseState(std::vector<int>&states,const AttributeSet&attss);
static std::vector<int>&get(int mask);
static std::vector<int> get(int mask);
};
}
#endif

View File

@ -3876,6 +3876,12 @@ bool View::hasFlag(int flag) const {
return (mViewFlags&flag)==flag;
}
void View::bringToFront() {
if (mParent != nullptr) {
mParent->bringChildToFront(this);
}
}
void View::onAttachedToWindow(){
mPrivateFlags3 &= ~PFLAG3_IS_LAID_OUT;
jumpDrawablesToCurrentState();

View File

@ -909,6 +909,7 @@ public:
// Parent and children views
virtual ViewGroup*getParent()const;
ViewGroup*getRootView()const;
void bringToFront();
virtual View* findViewById(int id)const;
virtual View* findViewWithTag(void*)const;

View File

@ -2630,6 +2630,17 @@ bool ViewGroup::dispatchVisibilityAggregated(bool isVisible) {
return isVisible;
}
void ViewGroup::bringChildToFront(View* child) {
const int index = indexOfChild(child);
if (index >= 0) {
removeFromArray(index);
addInArray(child, mChildren.size());
child->mParent = this;
requestLayout();
invalidate();
}
}
bool ViewGroup::requestLayoutDuringLayout(View* view){
return true;
}

View File

@ -244,6 +244,7 @@ public:
void setLayoutMode(int layoutMode);
ViewOverlay*getOverlay()override;
virtual void bringChildToFront(View*);
bool getClipChildren()const;
void setClipChildren(bool clipChildren);
bool getClipToPadding()const;

View File

@ -137,11 +137,6 @@ RefPtr<Canvas>Window::getCanvas(){
return canvas;
}
Window::~Window() {
dispatchDetachedFromWindow();
delete mAttachInfo;
}
void Window::onActive(){
LOGV("%p[%s]",this,getText().c_str());
}

View File

@ -65,7 +65,6 @@ public:
Window(Context*,const AttributeSet&);
void setRegion(const RefPtr<Region>&region);
void draw();
virtual ~Window();
virtual void setText(const std::string&);
const std::string getText()const;
virtual View& setPos(int x,int y)override;

View File

@ -250,8 +250,6 @@ void TextAppearanceAttributes::readTextAppearance(Context*ctx,const AttributeSet
TextView::TextView(Context*ctx,const AttributeSet& attrs)
:View(ctx,attrs){
initView();
mLayout=new Layout(20,50);
mHintLayout=new Layout(20,50);
setText(attrs.getString("text"));
Drawable* left =ctx->getDrawable(attrs,"drawableLeft");