alloc

Erlang, pass an nif object between functions

[亡魂溺海] 提交于 2019-12-05 15:26:57
I write a C nif code and in function new , it creates a stack struct with enif_alloc_resource and returns that. when i use function enif_make_resources , it always returns <<>> in erlang. Here is my C code: #include "erl_nif.h" static ErlNifResourceType *MEM_RESOURCE; typedef struct { int n; int ps; int pe; ERL_NIF_TERM *data; } Stack; Stack *new(ErlNifEnv *env, int size) { Stack *s = (Stack*) enif_alloc_resource(MEM_RESOURCE, sizeof(Stack)); s->n = size; s->ps = 0; s->pe = size - 1; s->data = enif_alloc(sizeof(ERL_NIF_TERM) * size); return s; } static ERL_NIF_TERM new_nif(ErlNifEnv* env, int

Using alloc, init in ARC enabled projects

怎甘沉沦 提交于 2019-12-05 06:43:28
Actually I am working on a project with ARC enabled. I know using alloc and init is taking ownership of the object. I know, If I create a string like this NSString *myString = [[NSString alloc]initWithFormat:@"Something"]; then I need to release the myString on myself. What If I am using ARC enabled? I cannot release on myself. So will it create a leak? Or should I don't create object like this? I can create a string like below code too. NSString *myString = [NSString stringWithFormat:@"Something"]; But which type I need to use exactly for ARC enabled project ? What will happen if I use first

iOS error: No visible @interface for 'xxxx' declares the selector 'alloc'

泪湿孤枕 提交于 2019-12-05 06:32:39
Here is my TextValidator class: //TextValidator.h #import <Foundation/Foundation.h> @interface TextValidator : NSObject - (BOOL) isValidPassword:(NSString *)checkPassword; - (BOOL) isValidEmail:(NSString *)checkString; - (BOOL) isEmpty:(NSString *)checkString; @end // TextValidator.m #import "TextValidator.h" @implementation TextValidator - (BOOL) isEmpty:(NSString *)checkString { return YES; } - (BOOL) isValidPassword:(NSString *)checkPassword { return YES; } - (BOOL) isValidEmail:(NSString *)checkString { return YES; } @end This is the way I try to initialise the TextValidator class in

init] in automatic reference counting

天大地大妈咪最大 提交于 2019-12-05 04:51:47
问题 I know that I am suppose to use: ObjectClass *tmpObject = [[ObjectClass alloc] init]; realObject = tmpObject; [tmpObject release] to initialise realObject (where realObject is an object within a class) But now with ARC mode, releasing is automatic, do i still need to use this technique? Can I simply use realObject = [[ObjectClass alloc] init]; ? If not is there any specific reason why it would leak? Thanks 回答1: As Spencer said, if you compile with ARC enabled, you cannot call release at all.

init] in automatic reference counting

倾然丶 夕夏残阳落幕 提交于 2019-12-03 21:16:37
I know that I am suppose to use: ObjectClass *tmpObject = [[ObjectClass alloc] init]; realObject = tmpObject; [tmpObject release] to initialise realObject (where realObject is an object within a class) But now with ARC mode, releasing is automatic, do i still need to use this technique? Can I simply use realObject = [[ObjectClass alloc] init]; ? If not is there any specific reason why it would leak? Thanks As Spencer said, if you compile with ARC enabled, you cannot call release at all. It is an error to do so and the compiler takes care of it for you. However: ObjectClass *tmpObject = [

Creating a singleton with allocWithZone:

青春壹個敷衍的年華 提交于 2019-11-30 14:13:03
问题 BNRItemStore is a singleton, and I was confused on why super allocWithZone: must be called instead of plain old super alloc . And then override alloc instead of allocWithZone . #import "BNRItemStore.h" @implementation BNRItemStore +(BNRItemStore *)sharedStore { static BNRItemStore *sharedStore = nil; if (!sharedStore) sharedStore = [[super allocWithZone: nil] init]; return sharedStore; } +(id)allocWithZone:(NSZone *)zone { return [self sharedStore]; } @end 回答1: [super alloc] will call through

Creating a singleton with allocWithZone:

巧了我就是萌 提交于 2019-11-30 09:59:08
BNRItemStore is a singleton, and I was confused on why super allocWithZone: must be called instead of plain old super alloc . And then override alloc instead of allocWithZone . #import "BNRItemStore.h" @implementation BNRItemStore +(BNRItemStore *)sharedStore { static BNRItemStore *sharedStore = nil; if (!sharedStore) sharedStore = [[super allocWithZone: nil] init]; return sharedStore; } +(id)allocWithZone:(NSZone *)zone { return [self sharedStore]; } @end [super alloc] will call through to allocWithZone: , which you've overridden to do something else. In order to actually get the superclass's

How to release static Objective-C variables

非 Y 不嫁゛ 提交于 2019-11-30 08:28:14
The StackOverflow question "using static keyword in objective-c when defining a cached variable" references code from Example 4 of Xcode's TableViewSuite that defines a static NSDateFormatter and calls alloc but never calls release . Shouldn't static variables be released? If yes, where in the code should they be released? If no, why not? Shouldn't static variables be released? If yes, where in the code should they be released? If no, why not? It depends. If the variable is initialized only once, and should stay around for the lifetime of the application, then no, it shouldn't be released (its

Is a malloc() needed before a realloc()?

假如想象 提交于 2019-11-30 05:56:55
Since I had read realloc will act as malloc if the size pointed is 0, I was using it without malloc(), provided the pointer was static, global, or explicitly set to NULL if automatic. However, I notice a lot of programmers try to set it or set it to malloc(1). Is it needed? From Open Groups' specifications : If ptr is a null pointer, realloc() shall be equivalent to malloc() for the specified size. If ptr does not match a pointer returned earlier by calloc(), malloc(), or realloc() or if the space has previously been deallocated by a call to free() or realloc(), the behavior is undefined.

what is difference between alloc and allocWithZone:?

拈花ヽ惹草 提交于 2019-11-28 15:47:23
From forum discussion , seem like that the big difference is performance factor, allocWithZone: will alloc memory from particular memory area, which reduce cost of swapping. In practice, almost get no chance to use allocWithZone: , anyone can give simple example to illustrate which case to use allocWithZone: ? Thanks, d11wtq When one object creates another, it’s sometimes a good idea to make sure they’re both allocated from the same region of memory. The zone method (declared in the NSObject protocol) can be used for this purpose; it returns the zone where the receiver is located. This