我一直试图找出如何找到保留该对象的东西。但是似乎再也没有引用它了,那么它如何保留呢?它在仪器和内存调试器中均显示为泄漏。
对于内存调试器来说,是否有可能没有被符号化的东西,以找出保留它的原因?是什么会导致像这样的孤立物体?
答案 0 :(得分:0)
您的对象可能会保留自身。使用闭包时很容易发生这种情况。确保像这样在闭包中显式地标注自己的弱点:
someFunc() { [weak self] in
// some code
self?.propertyOnSelf
}
答案 1 :(得分:-1)
这是一个艰难的过程,但我想我找到了答案。因此,我有一个AnyObjects([AnyObject])的通用数组,并且我“知道”这些是可哈希值。我当时的想法是可以做的:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package groupchatapp;
/**
*
* @author Jim Ng
*/
public class ChatInterface extends javax.swing.JFrame {
/**
* Creates new form GroupChatGUI
*/
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
TalkIcon = new javax.swing.JLabel();
TalkIcon1 = new javax.swing.JLabel();
usersLbl = new javax.swing.JLabel();
GroupsLbl = new javax.swing.JLabel();
GroupChatsLbl = new javax.swing.JLabel();
groupNameLbl = new javax.swing.JLabel();
groupLblWhiteBg = new javax.swing.JLabel();
userLblWhiteBg = new javax.swing.JLabel();
groupsScrollPane = new javax.swing.JScrollPane();
groupChatList = new java.awt.List();
groupNameScrollPane = new javax.swing.JScrollPane();
allMessagesTextField = new java.awt.TextField();
viewBtn = new java.awt.Button();
deleteChatBtn = new java.awt.Button();
editChatBtn = new java.awt.Button();
createNewBtn = new java.awt.Button();
typeMessageTextField = new java.awt.TextField();
sendBtn1 = new java.awt.Button();
addBtn1 = new java.awt.Button();
GardensByTheBayBG = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
TalkIcon.setBackground(new java.awt.Color(255, 255, 255));
TalkIcon.setForeground(new java.awt.Color(255, 255, 255));
TalkIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/groupchatapp/TalkIcon.png"))); // NOI18N
getContentPane().add(TalkIcon);
TalkIcon.setBounds(310, 0, 190, 200);
TalkIcon1.setBackground(new java.awt.Color(255, 255, 255));
TalkIcon1.setForeground(new java.awt.Color(255, 255, 255));
TalkIcon1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/groupchatapp/GroupIcon.png"))); // NOI18N
TalkIcon1.setPreferredSize(new java.awt.Dimension(30, 30));
getContentPane().add(TalkIcon1);
TalkIcon1.setBounds(250, 210, 50, 50);
usersLbl.setBackground(new java.awt.Color(255, 255, 255));
usersLbl.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
usersLbl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
usersLbl.setText("Users");
getContentPane().add(usersLbl);
usersLbl.setBounds(610, 210, 130, 50);
GroupsLbl.setBackground(new java.awt.Color(255, 255, 255));
GroupsLbl.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
GroupsLbl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
GroupsLbl.setText("Groups");
getContentPane().add(GroupsLbl);
GroupsLbl.setBounds(70, 200, 160, 70);
GroupChatsLbl.setBackground(new java.awt.Color(255, 255, 255));
GroupChatsLbl.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
GroupChatsLbl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
GroupChatsLbl.setText("Group Name");
getContentPane().add(GroupChatsLbl);
GroupChatsLbl.setBounds(240, 200, 370, 70);
groupNameLbl.setBackground(new java.awt.Color(255, 255, 255));
groupNameLbl.setForeground(new java.awt.Color(255, 255, 255));
groupNameLbl.setIcon(new javax.swing.ImageIcon(getClass().getResource("/groupchatapp/WhiteBackground.PNG"))); // NOI18N
groupNameLbl.setText("Ty");
getContentPane().add(groupNameLbl);
groupNameLbl.setBounds(240, 210, 360, 50);
groupLblWhiteBg.setBackground(new java.awt.Color(255, 255, 255));
groupLblWhiteBg.setForeground(new java.awt.Color(255, 255, 255));
groupLblWhiteBg.setIcon(new javax.swing.ImageIcon(getClass().getResource("/groupchatapp/WhiteBackground.PNG"))); // NOI18N
groupLblWhiteBg.setText("Ty");
getContentPane().add(groupLblWhiteBg);
groupLblWhiteBg.setBounds(70, 210, 160, 50);
userLblWhiteBg.setBackground(new java.awt.Color(255, 255, 255));
userLblWhiteBg.setForeground(new java.awt.Color(255, 255, 255));
userLblWhiteBg.setIcon(new javax.swing.ImageIcon(getClass().getResource("/groupchatapp/WhiteBackground.PNG"))); // NOI18N
userLblWhiteBg.setText("Ty");
getContentPane().add(userLblWhiteBg);
userLblWhiteBg.setBounds(610, 210, 130, 50);
groupsScrollPane.setViewportView(groupChatList);
getContentPane().add(groupsScrollPane);
groupsScrollPane.setBounds(70, 270, 160, 230);
allMessagesTextField.setFont(new java.awt.Font("Dialog", 0, 24)); // NOI18N
groupNameScrollPane.setViewportView(allMessagesTextField);
getContentPane().add(groupNameScrollPane);
groupNameScrollPane.setBounds(240, 270, 360, 190);
viewBtn.setBackground(new java.awt.Color(255, 0, 0));
viewBtn.setFont(new java.awt.Font("Dialog", 0, 24)); // NOI18N
viewBtn.setForeground(new java.awt.Color(255, 255, 255));
viewBtn.setLabel("View");
getContentPane().add(viewBtn);
viewBtn.setBounds(630, 460, 90, 40);
deleteChatBtn.setBackground(new java.awt.Color(255, 0, 0));
deleteChatBtn.setForeground(new java.awt.Color(255, 255, 255));
deleteChatBtn.setLabel("Delete");
getContentPane().add(deleteChatBtn);
deleteChatBtn.setBounds(120, 540, 70, 20);
editChatBtn.setBackground(new java.awt.Color(255, 0, 0));
editChatBtn.setForeground(new java.awt.Color(255, 255, 255));
editChatBtn.setLabel("Edit");
editChatBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
editChatBtnActionPerformed(evt);
}
});
getContentPane().add(editChatBtn);
editChatBtn.setBounds(160, 510, 70, 20);
createNewBtn.setBackground(new java.awt.Color(255, 0, 0));
createNewBtn.setForeground(new java.awt.Color(255, 255, 255));
createNewBtn.setLabel("Create");
createNewBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
createNewBtnActionPerformed(evt);
}
});
getContentPane().add(createNewBtn);
createNewBtn.setBounds(70, 510, 70, 20);
typeMessageTextField.setFont(new java.awt.Font("Dialog", 0, 24)); // NOI18N
typeMessageTextField.setText("type your message here...");
getContentPane().add(typeMessageTextField);
typeMessageTextField.setBounds(240, 460, 360, 40);
sendBtn1.setBackground(new java.awt.Color(255, 0, 0));
sendBtn1.setFont(new java.awt.Font("Dialog", 0, 24)); // NOI18N
sendBtn1.setForeground(new java.awt.Color(255, 255, 255));
sendBtn1.setLabel("Send");
getContentPane().add(sendBtn1);
sendBtn1.setBounds(510, 510, 90, 40);
addBtn1.setBackground(new java.awt.Color(255, 0, 0));
addBtn1.setFont(new java.awt.Font("Dialog", 0, 24)); // NOI18N
addBtn1.setForeground(new java.awt.Color(255, 255, 255));
addBtn1.setLabel("Add");
getContentPane().add(addBtn1);
addBtn1.setBounds(410, 510, 90, 40);
GardensByTheBayBG.setIcon(new javax.swing.ImageIcon(getClass().getResource("/groupchatapp/Singapore Garden City.jpg"))); // NOI18N
getContentPane().add(GardensByTheBayBG);
GardensByTheBayBG.setBounds(0, -210, 2260, 1090);
pack();
}// </editor-fold>
private void createNewBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void editChatBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ChatInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ChatInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ChatInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ChatInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ChatInterface().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel GardensByTheBayBG;
private javax.swing.JLabel GroupChatsLbl;
private javax.swing.JLabel GroupsLbl;
private javax.swing.JLabel TalkIcon;
private javax.swing.JLabel TalkIcon1;
private java.awt.Button addBtn1;
private java.awt.TextField allMessagesTextField;
private java.awt.Button createNewBtn;
private java.awt.Button deleteChatBtn;
private java.awt.Button editChatBtn;
private java.awt.List groupChatList;
private javax.swing.JLabel groupLblWhiteBg;
private javax.swing.JLabel groupNameLbl;
private javax.swing.JScrollPane groupNameScrollPane;
private javax.swing.JScrollPane groupsScrollPane;
private java.awt.Button sendBtn1;
private java.awt.TextField typeMessageTextField;
private javax.swing.JLabel userLblWhiteBg;
private javax.swing.JLabel usersLbl;
private java.awt.Button viewBtn;
// End of variables declaration
这可以编译并可以正常工作,但这实际上会导致内存泄漏!我假设Swift内部内部正在发生某些事情导致此问题。也许我可以将其放在一个密闭文件中,以证明泄漏。如果将每个Hashable值传递给AnyHashable(object),没有任何泄漏。
不幸的是,我仍然需要找到一种方法来强制我的通用AnyObject值可哈希化。