Objective C++ for loop multiple initialize fails - ios

So I traced a bug in Objective C++ for iOS to this code
for(int i = (maxBin - 4), max = 0; i <= (maxBin + 3); i++) {
max += (fftValLeft[i] * fftValLeft[i]);
}
The "max = 0;" doesn't happen. I moved the initialization before the loop and all is good now.
This is with Xcode 4.6.3. Is this somehow normal C behavior?

You are creating a new variable called max whose scope is only visible to the for loop.
So once you come out of the loop, max value that you compute inside the loop is not visible outside of it.
Hope that helps.

Related

Compute shader hangs when setting two pixels while looping RWTexture2D (DirectX11, SM5)

I'm trying to perform some basic cellular automata on compute shader (DirectCompute) but without double buffering, so I'm using unordered access view to a RWTexture2D<uint> for the data, however I'm having some really strange hang/crash here, I could make a very small snippet that produces the issue:
int w = 256;
for (int x = 0; x < w; ++x)
{
for (int y = 1; y < w; ++y)
{
if (map[int2(x, y - 1)])
{
map[int2(x, y)] = 10;
map[int2(x, y-1)] = 30;
}
}
}
where map is RWTexture2D<uint>.
If I remove the if or one of the assignments, it works, I thought it could be some kind of limit so I tried looping just 1/4 of the texture but the problem persists. That code is dispatched with (1,1,1) and kernel numthreads is (1,1,1) too, in my real-world scenario I want to loop from bottom to top and fill the voids (0) with the pixel I'm currently looping (think of a "falling sand" kind of effect), so it can't be parallel except in columns since it depends on the bottom pixel.
I don't understand what is causing the shader to hang though, there's no error or anything, it simply hangs and never not even times out.
EDIT:
After some further investigation, I came across something really intriguing; when I pass that w value in a constant buffer it all works fine. I have no idea what would cause that, maybe it's some compiling optimization that went wrong, maybe it tries to unroll the loop what causes some issue, and passing the value in a constant buffer disables that, however I'm compiling the shaders in debug with no optimization so I don't know.
I've had issues declaring variables in global scope like this before. I believe it's because it's not static const (so declare as a static const and it should work). Most likely, it's treating it as a constant buffer (with some default naming) and the contents are undefined since you're not binding a buffer, which causes undefined results. So the following code should work:
static const int w = 256;
for (int x = 0; x < w; ++x)
{
for (int y = 1; y < w; ++y)
{
if (map[int2(x, y - 1)])
{
map[int2(x, y)] = 10;
map[int2(x, y-1)] = 30;
}
}
}

Modifying SCNParticleSystem colors in an SCNParticleEventBlock doesn't work

Given the sample code provided for handle(_:forProperties:handler:), the following block should turn green particle red, but doesn't:
[system handleEvent:SCNParticleEventBirth
forProperties:#[SCNParticlePropertyColor]
withBlock:^(void **data, size_t *dataStride, uint32_t *indices , NSInteger count) {
for (NSInteger i = 0; i < count; ++i) {
float *color = (float *)((char *)data[0] + dataStride[0] * i);
if (rand() & 0x1) { // Switch the green and red color components.
color[0] = color[1];
color[1] = 0;
}
}
}];
With the following, I am able to see green particles, but no matter what I do, I can't seem to get various SCNParticlePropertys to do anything.
SCNParticleSystem *system = [SCNParticleSystem particleSystem];
system.particleColor = NSColor.greenColor;
system.birthRate = 1;
An interesting observation is that in the block above, dataStride only appears to have values for certain SCNParticlePropertys. position, angle, velocity, and angularVelocity all yield 16, life and frame yield 4, and all others show nil. I can only assume that either the API allows per-particle adjustment for a few properties, or additional configuration must be done on my SCNParticleSystem instance in order to "unlock" modification via a SCNParticleEventBlock block.
The above produces the same results in Swift, and both Xcode 8 and 9. I've tried assigning values to nearly every property of my SCNParticleSystem with no luck, and do not see any sample code provided by Apple other than what's in the header.
Thanks for any help.
For anyone stumbling across this question: check out the particle slide in the WWDC 2014 SceneKit sample project, they provide example for basically everything you can do with SceneKit.
As expected, the property that needed a value for this to work was particleColorVariation on my SCNParticleSystem instance. Setting it to anything besides SCNVector4Zero (eg. SCNVector4Make(0, 0, 0, 1)) results in dataStride in SCNParticleEventBlock having a non-nil value (16) at the index of the particle property.
It's unfortunate that this appears to be an undocumented requirement, so hopefully this post is useful for anyone running into this issue in the future.

Have issue with pretty simple C code

I am developing an app in XCode and have to write a bit of C for an algorithm. Here is a part of the C code:
double dataTag[M][N];
// dataTag initialized to values.....
double w[N]; // This is outside for loop at the top level of the method
for (int i = 0; i < N; i++) {
w[i] = pow(10.0, dataTag[2][i] / 10.0 / b);
}
//This is inside for loop.....
double disErr[N];
// disErr set and values confirmed with printArray...
double transedEstSetDrv[N][M];
// transedEstSetDrv set and values confirmed with printArray...
double stepGrad[M] = {0, 0, 0};
for (int j = 0; j < M; j++) {
double dotProductResult[M];
dotProductOfArrays(w, disErr, dotProductResult, N);
stepGrad[j] = sumOfArrayMultiplication(transedEstSetDrv[j], dotProductResult, M);
}
// Print array to console to confirm values
NSLog(#"%f %f %f", stepGrad[0], stepGrad[1], stepGrad[2]); <-- if this is present algorithm gives different results.
//Continue calculations......
So this is a part of algorithm in C which is inside for loop. The weird part is the NSLog that prints stepGrad array. Depending if i comment the call to the NSLog or not - the algorithm as a whole gives different results.
It would be great if someone gave some debugging suggestions.
Thanks!
UPDATE 1:
Simplified example which has the same issue and gave more code to support the issue.
UPDATE 2:
Removed the length_of_array function and just replaced it with a known number for simplicity.
So i will answer my own question.
Thanks to the comment from #Klas Lindbäck, i fixed the issue which was related to not initializing a C static array in for loop. So i went over all arrays before and after the code that had issue and did a
memset(a_c_array, 0, sizeof(a_c_array));
after declaration of each array. That is now working fine. Thank you for all your help!

SIGSEGV Error occurs on the actual device but not on ios simulator

I am the developer of a cydia tweak named CountdownCenter. I was trying to create a digital clock appearance for my widget, so I created a sample app that does this perfectly. After doing this, I transferred this data into the code of my tweak but, this causes a SIGSEGV crash on my iPhone. After some testing, I found the part that is responsible for the crash, but I just can't find what's wrong as this part would work on a normal app. Can you help me please?
Here is the code:
int digitarray[10];
int c = 0;
digitarray[0] = 0;
digitarray[1] = 0;
while (secon>0) {
int digitt = secon % 10;
digitarray[c] = digitt;
secon /= 10;
c++;
}
lbl.text = [NSString stringWithFormat:#"%d", digitarray[0]];
[self selectimage:digitarray[0] img:numview10];
SIGSEGV usually means, that you're trying to access memory, that you are not allowed to access. (btw are you testing this with a release build?) Maybe f.e. here (there are some other similar places too):
c = 0;
while (secon>0) {
int digitt = secon % 10;
digitarray[c] = digitt;
secon /= 10;
c++;
}
There are some possibilities:
secon (horrible variable names btw...) is a float or double, in that case the while loop is either never entered or never left
c becomes so large that it's beyond digitarray's bounds
To solve this issue I would recommend to but a breakpoint at the beginning of your code and check it step by step.

AS2 - Trying to create a grid of boxes

This is my current code:
_root.createEmptyMovieClip("noteGrid", _root.getNextHighestDepth());
for(i = 1; i <= 14; i++){
currentBlock = _root.noteGrid.attachMovie("block", "block" + i, _root.noteGrid.getNextHighestDepth);
currentBlock._x = Math.floor(i / 7) * 25;
currentBlock._y = (i % 7) * 25;
}
I have a movieclip with linkage set to block. When I compile this, the block appears however they are all on top of each other. When I used trace commands to find currentBlock._x, they are the correct values.
The problem lies with your setting of depths.
_root.noteGrid.getNextHighestDepth
You are trying to access a property of noteGrid, if you trace it you will see it tells you it is a function, rather than calling a function. To call the function do
_root.noteGrid.getNextHighestDepth()
By the looks of things your code isn't quite what you want but that can't really be fixed without you giving more details about what you're trying to do. Assuming you're trying to make a 2x7 grid, then you'll want to change your for loop to
for(i = 0; i < 14; i++)

Resources