memory

What's the best practice to prevent memory leak if an exception thrown in constructor?

旧街凉风 提交于 2020-01-12 04:41:33
问题 I know if an exception is thrown in constructor, destructor will not be called(simple class, no inheritance). So if an exception is thrown in constructor and there is a chance some heap memory is not cleaned up. So what's best practice here? let's assume I have to call some function in constructor and it may throw exception. Shall I always use shared pointer in this case? What's the alternatives? Thank you! 回答1: I would stick to the RAII idiom. If you avoid "naked" resources (like operator

SpriteKit not deallocating all used memory

纵饮孤独 提交于 2020-01-12 03:56:08
问题 I have ready many (if not all) articles on SO and other sites about the disasters of dealing with SpriteKit and memory issues. My problem, as many others have had, is after i leave my SpriteKit scene barely any of the memory added during the scene session is released. I've tried to implement all suggested solutions in the articles i've found, including, but not limited to... 1) Confirm the deinit method is called in the SKScene class. 2) Confirm no strong references to the parent VC in the

Memory warning after using the UIImagePicker once

天大地大妈咪最大 提交于 2020-01-12 03:10:11
问题 I've referred to this very good reference: https://stackoverflow.com/questions/1282830/uiimagepickercontroller-uiimage-memory-and-more but I'm having some very serious issues. After I take a photo, I receive a memory warning. This is for the first photo I take, not the second or third. I was wondering if it's because I've got a couple of small jpegs loaded from the application directory into scrolling views. The only solution I can think of is to unload everything in my mainview whilst the

Objective-C memory management, xml parser and other non-trivial examples

二次信任 提交于 2020-01-12 02:24:47
问题 I know the basic principles about memory management (retain count, autorelease pools etc) in Cocoa, but once you go past simple retain/release, it's getting a bit more confusing. I couldn't find decent answers for those, as most tutorials cover simple scenarios. I would like to ask about best practices in how to write the code and avoid leaks. 1st question - iterations and temporary assignments: for (id object in objectArray) { Model *currentItem = object; /* do something with currentItem */

React.js app using up a lot of memory ( almost double the original implementation )

百般思念 提交于 2020-01-12 01:49:06
问题 I recently ported a heavy page to React. I've kept the html almost identical. The main difference being that, earlier, the server rendered html was directly given to the browser and now, the react rewrite pulls json via a server side API and uses React to manage the DOM. I've seen heap snapshots for the earlier implementation going up to 55 MBs . For the same data, the heap snapshot for the React.js implementation comes to around 100+ MBs (almost double) I understand that the json data held

Download Doc/PDF from the internet and save to internal memory [closed]

微笑、不失礼 提交于 2020-01-12 01:46:49
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago . I am attempting to make an application that will primarily open and read a predetermined set of documents that I have. The original idea was for all the documents (about 40 equaling MB) to be copied into the user's internal memory (/data/data/(appname)/files) on first run or

Designing a Queue to be a shared memory

拈花ヽ惹草 提交于 2020-01-12 01:43:26
问题 I'm attempting to design/implement a (circular) queue (in C) as a shared memory so that it can be shared between multiple threads/processes. The queue structure is as follows: typedef struct _q { int q_size; int q_front; int q_rear; int *q_data; }queue; Which supports the following functions: int empty_q(queue *q); int display_q(queue *q); int create_q(queue **q, int size); int delete_q(queue **q); int enqueue(queue *q, int data); int dequeue(queue *q, int *data); As per the queue size

C++ int vs long long in 64 bit machine

旧巷老猫 提交于 2020-01-12 01:13:54
问题 My computer has 64 bit processor and when I look for sizeof(int) , sizeof(long) , and sizeof(long long) , it turns out that int and long are 32 bits, and long long is 64 bit. I researched the reason, and it appears that popular assumption telling that int in C++ fits machine's word size is wrong. As I understood it is up to compiler to define what will be the size, and mine is Mingw-w64. The reason for my research was understanding that if the usage of types smaller than word size is

C++ int vs long long in 64 bit machine

房东的猫 提交于 2020-01-12 01:13:42
问题 My computer has 64 bit processor and when I look for sizeof(int) , sizeof(long) , and sizeof(long long) , it turns out that int and long are 32 bits, and long long is 64 bit. I researched the reason, and it appears that popular assumption telling that int in C++ fits machine's word size is wrong. As I understood it is up to compiler to define what will be the size, and mine is Mingw-w64. The reason for my research was understanding that if the usage of types smaller than word size is

C++多线程——原子操作atomic

半世苍凉 提交于 2020-01-11 22:36:37
1. 原子操作 1.1 示例 原子操作 是个不可分割的操作。 在系统的所有线程中,你是不可能观察到原子操作完成了一半这种情况的; 它要么就是做了,要么就是没做,只有这两种可能。 不使用原子操作: # include <iostream> # include <thread> # include <atomic> using namespace std ; long num = 0 ; void addnum ( ) { for ( int i = 0 ; i < 100000 ; i ++ ) num ++ ; //不对全局变量进行互斥访问 } int main ( ) { int nthreads = 2 ; thread t [ nthreads ] ; for ( int i = 0 ; i < nthreads ; i ++ ) t [ i ] = thread ( addnum ) ; for ( auto & th : t ) th . join ( ) ; cout << num << endl ; return 0 ; } 输出结果: 最终结果为 109515 ,这个结果小于 200000 ,说明在对全局变量进行写的时候出现了下面的情况: 明明加了两次,但是因为访问不是互斥的,从而导致实际的值小。 使用原子操作可以避免这种情况的发生