-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest2.cpp
124 lines (94 loc) · 2.09 KB
/
test2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "stdio.h"
#include "string.h"
#include "RefBase.h"
#include "StrongPointer.h"
#ifndef ALOGD
#define ALOGD printf
#endif
namespace android{
class Bigclass : public RefBase
{
public:
Bigclass(const char *name){
strcpy(mName, name);
ALOGD(">>>Construct:(%p) %s\n", this, mName);
}
~Bigclass(){
ALOGD("<<<destruct: (%p) %s\n", this, mName);
}
void setStrongRefs(sp<Bigclass> b){
spB = b;
}
void setWeakRefs(sp<Bigclass> b) {
wpB = b;
}
private:
sp<Bigclass> spB;
wp<Bigclass> wpB;
char mName[64];
};
}
using namespace android;
void testStrongCrossRef(){
sp<Bigclass> A = new Bigclass("testStrongClassA");
printf("-------\n");
sp<Bigclass> B = new Bigclass("testStrongClassB");
printf("-------\n");
A->setStrongRefs(B);
printf("-------\n");
B->setStrongRefs(A);
printf("-------\n");
}
void testWeakCrossRef() {
sp<Bigclass> A = new Bigclass("testWeakClassA");
sp<Bigclass> B = new Bigclass("testWeakClassB");
printf("A: *[%p] B: *[%p]\n", &A, &B);
printf("-------\n");
A->setStrongRefs(B);
printf("-------\n");
B->setWeakRefs(A);
printf("-------\n");
}
void testCrossRef() {
sp<Bigclass> A = new Bigclass("testNormalClassA");
sp<Bigclass> B = new Bigclass("testNormalClassB");
A->setWeakRefs(B);
B->setWeakRefs(A);
}
void testW() {
Bigclass *p = new Bigclass("PPPP");
wp<Bigclass> A;
{
printf("in\n");
wp<Bigclass> B(p);
wp<Bigclass> C(p);
A = B;
printf("out\n");
}
wp<Bigclass> D = A;
}
void testS() {
Bigclass *p = new Bigclass("QQQQ");
{
sp<Bigclass> A(p);
sp<Bigclass> B(p);
sp<Bigclass> C(p);
printf("out\n");
}
sp<Bigclass> D(p);
}
int main(){
ALOGD("* Start testStrongClasses..\n");
// testStrongCrossRef();
ALOGD("* testStrongClasses Should be destructed!!\n\n");
ALOGD("* Start testWeakClasses..\n");
testWeakCrossRef();
ALOGD("* testWeakClasses Should be destructed!!\n\n");
ALOGD("* Start testNormalClasses..\n");
// testCrossRef();
ALOGD("* testNormalClasses should be destructed!!\n\n");
// printf("segment fault \n";
//testW();
//testS();
return 0;
}