如何在这样的关系中避免内存泄漏?
@class Node;
@interface Node : NSObject {
Node *parent;
Node *child;
id object;
}
-(id)initWithObject:(id)anObject;
-(id)object;
-(void)setChild:(Node *)aNode;
-(void)setParent:(Node *)aNode;
@end
@implementation Node
-(id)initWithObject:(id)anObject {
if (self = [self init]) {
object = [anObject retain];
}
return self;
}
-(id)object {
return object;
}
-(void)setParent:(Node *)aNode {
[parent release];
parent = [aNode retain];
}
-(void)setChild:(Node *)aNode {
[child release];
child = [aNode retain];
[child setParent:self];
}
-(void)dealloc {
[child release];
[parent release];
[super dealloc];
}
@end
Run Code Online (Sandbox Code Playgroud)
Node *root = …Run Code Online (Sandbox Code Playgroud) 我写了这个简单的Prolog程序.
man(socrates).
mortal(X) :- man(X).
immortal(X) :- immortal(X).
Run Code Online (Sandbox Code Playgroud)
我问过常见的问题,比如苏格拉底是男人还是苏格拉底是凡人.
?- man(socrates).
true. //we know for a fact that Socrates is a man
?- mortal(socrates).
true. //and it can logically be inferred that Socrates is mortal
?- immortal(socrates).
//but we can't seem to figure out if he's immortal
Run Code Online (Sandbox Code Playgroud)
由于递归定义,它崩溃了immortal.循环引用也会使其崩溃或出错Out of stack space.
在我看来,至少在这种情况下,Prolog先生得出的结论是,根据该计划的规则,不能推断苏格拉底是不朽的,这是相当微不足道的.怎么样?我想它可以检查堆栈并查看它是否遍历已经遍历的规则.
有没有原因尚未实施?这样做是否有问题我可以忽略,或者是否已经执行此类分析的Prolog实现?
我理解为什么结构不能包含导致逻辑内存问题的循环引用,但为什么可空引用不能绕过这个限制呢?例如:
struct Foo
{
Foo? bar;
}
Run Code Online (Sandbox Code Playgroud)
显然这很容易导致堆栈溢出和循环引用,如果不小心,但不bar应该是指向另一个Foo实例的指针,默认为null?或者(更有可能)我不明白在内存中如何排列可空的值类型?
(我的背景知识主要包括来自这个问题和答案的信息.)
我正在尝试在 PHP 中复制 Excel 的循环引用公式。
在 Excel 中,我有:
A19 = A25-A22 (result: 8771.65)
A22 = A19*14.1% (result: 1236.80)
A25 = 10000
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试在 PHP 中计算它时,它没有给我正确的结果:
$Tax = 0;
$Gross = 0;
$Net_Amount = 10000;
$Gross = $Net_Amount - $Tax;
$Tax = $Gross * (14.1/100);
Run Code Online (Sandbox Code Playgroud)
关于如何在 PHP 中执行此操作的任何想法?
我在refs中有两个映射,并希望在一个事务中将它们相互关联.
我的功能看起来像这样:
(defn assoc-two
[one two]
(let [newone (assoc @one :two two)
newtwo (assoc @two :one one)]
(ref-set one newone)
(ref-set two newtwo)))
Run Code Online (Sandbox Code Playgroud)
现在我这样打电话assoc-two:
(dosync (assoc-two (ref {}) (ref {})))
Run Code Online (Sandbox Code Playgroud)
我现在得到和StackOverflowError.
我也试过这个:
(defn alter-two
[one two]
(alter one assoc :two two)
(alter two assoc :one one))
Run Code Online (Sandbox Code Playgroud)
有没有这样做的方式one有一个条目参考two,反之亦然,仍然在一个交易中?
我们的应用程序广泛使用Spring Beans,我们随机看到应用程序启动错误说有循环Bean依赖。但是这个错误并不总是发生,而是在多次重启中随机发生。这里随机性的原因是什么?如果存在循环依赖,为什么它不会始终失败/成功?
异常:org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名为“x”的 bean 时出错:请求的 bean 当前正在创建中:是否存在无法解析的循环引用?
通过使用graphql-js,我需要通过迭代某些数据的数组来动态创建graphql模式,例如:
[{
name: 'author',
fields: [{
field: 'name'
}, {
field: 'books',
reference: 'book'
}]
}, {
name: 'book',
fields: [{
field: 'title'
}, {
field: 'author',
reference: 'author'
}]
}]
Run Code Online (Sandbox Code Playgroud)
问题是循环引用.当我创建AuthorType时,我需要创建BookType,反之亦然.
因此生成的模式应如下所示:
type Author : Object {
id: ID!
name: String,
books: [Book]
}
type Book : Object {
id: ID!
title: String
author: Author
}
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?
每当我创建稍后可以重用的 DRY 函数,然后在模型中使用它们时,我都会得到循环引用;
例如:
我有以下型号:
from social.services import get_top_viewed_posts
class Post(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
title = models.CharField('Post Title', max_length=255)
class ActivityUpdateEmail(models.Model):
sent = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now = True)
def send(self):
posts = get_top_viewed_posts()
Run Code Online (Sandbox Code Playgroud)
我查看最多的帖子功能是另一个名为 services.py 的文件,因此我可以在其他地方访问它。看起来像:
from social.models import Post
def get_top_viewed_posts():
posts = Post.objects.filter(
pk__in=popular_posts_ids,
).order_by(
'-created_at'
)
return posts
Run Code Online (Sandbox Code Playgroud)
然后我得到错误:
Run Code Online (Sandbox Code Playgroud)services.py", line 1, in <module> from social.models import Post ImportError: cannot import name 'Post'
如果我把它改成:
transactions = Action.objects.filter( content_type__pk=35,created_at__gte=start_date, ).values_list('object_id', flat=True)
popular_posts_ids = …Run Code Online (Sandbox Code Playgroud) 我想知道在OCaml中是否有可能在同一记录的另一个字段中使用一个记录字段.
基本上,我有字段功能,我想在其中使用同一记录的其他,值,字段,所以当值更改时,函数将使用新值.
我可以设置功能字段mutable并在创建记录后更新它,例如
type 'a cell =
{ mutable value: 'a
; mutable fn: unit -> 'a }
let create_cell ~(value : 'a) : 'a cell =
let c = {value; fn= (fun () -> value + 42)} in
let _ = c.fn <- (fun () -> c.value + 42) in
c
Run Code Online (Sandbox Code Playgroud)
我想知道是否有可能没有fn字段是可变的并且一气呵成.
以下代码在receiverOptions和模板之间产生意外的循环依赖:
令人惊讶的是,如果从 spring 上下文中删除 kafkaProps,它会起作用。
看起来某些自动配置正在添加从模板到接收器选项的不必要的依赖项。
请建议配置 ReactiveKafkaConsumerTemplate 的正确方法。
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public Map<String, Object> kafkaProps() {
return Map.of(
ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092",
ConsumerConfig.GROUP_ID_CONFIG, DemoApplication.class.getSimpleName()
);
}
@Bean
public ReceiverOptions<String, String> receiverOptions(Map<String, Object> kafkaProps) {
return ReceiverOptions.<String, String>create(kafkaProps)
.withKeyDeserializer(new StringDeserializer())
.withValueDeserializer(new StringDeserializer())
.subscription(List.of("test-topic"));
}
@Bean
public ReactiveKafkaConsumerTemplate<String, String> template(ReceiverOptions<String, String> receiverOptions) {
return new ReactiveKafkaConsumerTemplate<>(receiverOptions);
}
@Bean
public ApplicationRunner runner(ReactiveKafkaConsumerTemplate<String, String> kafkaReceiver) {
return args -> kafkaReceiver.receive()
.log()
.subscribe();
} …Run Code Online (Sandbox Code Playgroud)