# Same pointer is valid in BP but null in C++

**URL:** <https://forums.unrealengine.com/t/same-pointer-is-valid-in-bp-but-null-in-c/423903>\
**Category:** Blueprint\
**Tags:** nullptr, unreal-engine, pointer, null, Blueprint, question, CPP\
**Created:** [April 29, 2018, 7:12pm UTC](https://forums.unrealengine.com/t/same-pointer-is-valid-in-bp-but-null-in-c/423903 "2018-04-29T19:12:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arty-McLabin](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arty-mclabin/32/1012076_2.png) [@Arty-McLabin](https://forums.unrealengine.com/u/Arty-McLabin)\
**Post date:** [April 29, 2018, 7:12pm UTC](https://forums.unrealengine.com/t/same-pointer-is-valid-in-bp-but-null-in-c/423903/1 "2018-04-29T19:12:18Z")

</div>

The next uproperty component pointer:

```
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
USkeletalMeshComponent* mesh;

```

constructor code:

```
mesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("mesh")); //succeeds all the time. checked.

```

Is returning a valid value in BP but null in C++.   
  
Even calling a wrapper C++ ufunction brings the same effect:

```
UFUNCTION(BlueprintCallable, BlueprintPure)
UMeshComponent* getMesh(){return mesh;} 

```

When called in C++ it’s nullptr, but is valid in BP. The mesh exists and is seen in the world.

## Important:
What is even more strange - is the fact that this behaviour occurs randomly with around 10% chance every play in PIE (without recompiling), while other 90% plays are completely fine both in BP and C++.

---

<div class="post-metadata">

**Author:** ![goncasmage](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@goncasmage](https://forums.unrealengine.com/u/goncasmage)\
**Post date:** [April 29, 2018, 7:38pm UTC](https://forums.unrealengine.com/t/same-pointer-is-valid-in-bp-but-null-in-c/423903/2 "2018-04-29T19:38:31Z")

</div>

Where are you accessing that variable in C++ (what function)?

---

<div class="post-metadata">

**Author:** ![Arty-McLabin](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arty-mclabin/32/1012076_2.png) [@Arty-McLabin](https://forums.unrealengine.com/u/Arty-McLabin)\
**Post date:** [April 29, 2018, 9:50pm UTC](https://forums.unrealengine.com/t/same-pointer-is-valid-in-bp-but-null-in-c/423903/3 "2018-04-29T21:50:02Z")

</div>

Are you sure you have tried to answer the right question? it has nothing to do with mine…

---

<div class="post-metadata">

**Author:** ![Arty-McLabin](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arty-mclabin/32/1012076_2.png) [@Arty-McLabin](https://forums.unrealengine.com/u/Arty-McLabin)\
**Post date:** [April 29, 2018, 9:52pm UTC](https://forums.unrealengine.com/t/same-pointer-is-valid-in-bp-but-null-in-c/423903/4 "2018-04-29T21:52:00Z")

</div>

I’m accessing that on tick, without a network.

---

<div class="post-metadata">

**Author:** ![goncasmage](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@goncasmage](https://forums.unrealengine.com/u/goncasmage)\
**Post date:** [April 29, 2018, 9:57pm UTC](https://forums.unrealengine.com/t/same-pointer-is-valid-in-bp-but-null-in-c/423903/5 "2018-04-29T21:57:27Z")

</div>

Hmm that is weird… i don’t really know what’s up with that…

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [April 30, 2018, 2:28am UTC](https://forums.unrealengine.com/t/same-pointer-is-valid-in-bp-but-null-in-c/423903/6 "2018-04-30T02:28:22Z")

</div>

Put breakpoint on it and do debug, breakpoint will be triggered by blueprint call too

---

<div class="post-metadata">

**Author:** ![goncasmage](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@goncasmage](https://forums.unrealengine.com/u/goncasmage)\
**Post date:** [April 30, 2018, 9:58am UTC](https://forums.unrealengine.com/t/same-pointer-is-valid-in-bp-but-null-in-c/423903/7 "2018-04-30T09:58:49Z")

</div>

Well, i’d say just put a null check in your C++ 😕

---

<div class="post-metadata">

**Author:** ![Arty-McLabin](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arty-mclabin/32/1012076_2.png) [@Arty-McLabin](https://forums.unrealengine.com/u/Arty-McLabin)\
**Post date:** [April 30, 2018, 10:26am UTC](https://forums.unrealengine.com/t/same-pointer-is-valid-in-bp-but-null-in-c/423903/8 "2018-04-30T10:26:39Z")

</div>

there is a null-check already, but the point is that i have nothing to do with the null case because it’s completely illogical. the mesh exists in the world and i should be able to access it :[

---

<div class="post-metadata">

**Author:** ![Arty-McLabin](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arty-mclabin/32/1012076_2.png) [@Arty-McLabin](https://forums.unrealengine.com/u/Arty-McLabin)\
**Post date:** [April 30, 2018, 10:27am UTC](https://forums.unrealengine.com/t/same-pointer-is-valid-in-bp-but-null-in-c/423903/9 "2018-04-30T10:27:54Z")

</div>

@anonymous_user_f5a50610 I honestly can’t think of a way to debug this. the pointer is null for seemingly random reason

---

<div class="post-metadata">

**Author:** ![MrMcKvarz](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MrMcKvarz](https://forums.unrealengine.com/u/MrMcKvarz)\
**Post date:** [May 1, 2018, 4:31pm UTC](https://forums.unrealengine.com/t/same-pointer-is-valid-in-bp-but-null-in-c/423903/10 "2018-05-01T16:31:35Z")

</div>

Could it be that you trying to return UMeshComponent instead of USkeletalMeshComponent without casting it first?

---

<div class="post-metadata">

**Author:** ![trdwll](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@trdwll](https://forums.unrealengine.com/u/trdwll)\
**Post date:** [May 2, 2018, 3:11am UTC](https://forums.unrealengine.com/t/same-pointer-is-valid-in-bp-but-null-in-c/423903/11 "2018-05-02T03:11:52Z")

</div>

/\*\* Get the mesh ._/  
UFUNCTION(BlueprintPure, Category = “Whatever”)  
FORCEINLINE USkeletalMeshComponent_ GetMesh() const { return mesh; }

---

<div class="post-metadata">

**Author:** ![Arty-McLabin](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arty-mclabin/32/1012076_2.png) [@Arty-McLabin](https://forums.unrealengine.com/u/Arty-McLabin)\
**Post date:** [May 2, 2018, 11:17am UTC](https://forums.unrealengine.com/t/same-pointer-is-valid-in-bp-but-null-in-c/423903/12 "2018-05-02T11:17:31Z")

</div>

It’s not necessary to cast child class to parent class in C++. a USkeletalMeshComponent inherits UMeshComponent so is a completely legal UMeshComponent variable.

---

<div class="post-metadata">

**Author:** ![Arty-McLabin](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arty-mclabin/32/1012076_2.png) [@Arty-McLabin](https://forums.unrealengine.com/u/Arty-McLabin)\
**Post date:** [May 2, 2018, 11:19am UTC](https://forums.unrealengine.com/t/same-pointer-is-valid-in-bp-but-null-in-c/423903/13 "2018-05-02T11:19:26Z")

</div>

I’ll try that when i get to coding, but it doesn’t look that it would work.   
  
I will check that anyway and reply if it works, thank you.

---

<div class="post-metadata">

**Author:** ![Arty-McLabin](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arty-mclabin/32/1012076_2.png) [@Arty-McLabin](https://forums.unrealengine.com/u/Arty-McLabin)\
**Post date:** [May 6, 2018, 11:16pm UTC](https://forums.unrealengine.com/t/same-pointer-is-valid-in-bp-but-null-in-c/423903/14 "2018-05-06T23:16:47Z")

</div>

Okay as expected, it does not work. Are you sure you did read the question?

---

<div class="post-metadata">

**Author:** ![trdwll](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@trdwll](https://forums.unrealengine.com/u/trdwll)\
**Post date:** [May 7, 2018, 6:16am UTC](https://forums.unrealengine.com/t/same-pointer-is-valid-in-bp-but-null-in-c/423903/15 "2018-05-07T06:16:10Z")

</div>

I did read the question, but perhaps I didn’t understand it.

I created a new class and added the following to the header.

![240162-1.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/e/8/5e897632de2b2cc9cd724d150da9fd34c11301d0.png)

I then added the rest in the cpp file.

 ![240163-2.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/1/d/11d28fff48848c95bf0c4be531cf0524ab989c39.png)

Then I created the BP and added it to my level with the following BP in the actor.

 ![240164-3.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/9/6/59688c741dfb391e0d457541037edb5178b7922e.png)

Here’s the output.

![240165-4.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/9/b/89b508444888129545047c6c38504c611dd4d256.png)

If I still don’t understand the question or this doesn’t work I apologize.

---

<div class="post-metadata">

**Author:** ![Arty-McLabin](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arty-mclabin/32/1012076_2.png) [@Arty-McLabin](https://forums.unrealengine.com/u/Arty-McLabin)\
**Post date:** [May 7, 2018, 10:35am UTC](https://forums.unrealengine.com/t/same-pointer-is-valid-in-bp-but-null-in-c/423903/16 "2018-05-07T10:35:39Z")

</div>

Thanks for the detailed answer, however my code for that works too, perhaps yout have missed the important note: “the fact that this behaviour occurs randomly with around 10% chance every play in PIE (without recompiling), while other 90% plays are completely fine both in BP and C++.”

---

<div class="post-metadata">

**Author:** ![trdwll](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@trdwll](https://forums.unrealengine.com/u/trdwll)\
**Post date:** [May 7, 2018, 6:22pm UTC](https://forums.unrealengine.com/t/same-pointer-is-valid-in-bp-but-null-in-c/423903/17 "2018-05-07T18:22:04Z")

</div>

No problem, I didn’t miss that either (it’s just a weird thing that’s happening). Could you post your code like I did above so I can see if you missed anything?

---

<div class="post-metadata">

**Author:** ![Arty-McLabin](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arty-mclabin/32/1012076_2.png) [@Arty-McLabin](https://forums.unrealengine.com/u/Arty-McLabin)\
**Post date:** [May 13, 2018, 3:26pm UTC](https://forums.unrealengine.com/t/same-pointer-is-valid-in-bp-but-null-in-c/423903/18 "2018-05-13T15:26:16Z")

</div>

Sorry for the delay.

My code is unfortunately 3000 lines of code and the calling sequences are too long to post them all.

All the relevant code is pretty much there in the question. it seems like a very nasty bug :[

---

<div class="post-metadata">

**Author:** ![trdwll](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@trdwll](https://forums.unrealengine.com/u/trdwll)\
**Post date:** [May 13, 2018, 9:29pm UTC](https://forums.unrealengine.com/t/same-pointer-is-valid-in-bp-but-null-in-c/423903/19 "2018-05-13T21:29:40Z")

</div>

It’s kinda hard to debug code without seeing any. Could you post the code on pastebin?

---

<div class="post-metadata">

**Author:** ![Arty-McLabin](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arty-mclabin/32/1012076_2.png) [@Arty-McLabin](https://forums.unrealengine.com/u/Arty-McLabin)\
**Post date:** [May 14, 2018, 10:53am UTC](https://forums.unrealengine.com/t/same-pointer-is-valid-in-bp-but-null-in-c/423903/20 "2018-05-14T10:53:33Z")

</div>

I understand you. Can’t do post the entire code though, as it’s too private.

Anyway thank you for your help, i guess i will have to try harder on myself
