Compiler optimization Trap--Global pointer multiple use exception

Source: Internet
Author: User

Do program development will be with the compiler to deal with, compiler optimization can give us code to run a certain increase, but there may be some unexpected problems. Here is the development time I encountered a pit, hoping to give you some reference

Speak directly to the code.

1 StaticUnsignedChar* S_data = NULL;//storing one frame of video data2 voiddorendering ()3 {4     //D3d11 Case5     if(S_devicetype = = Kunitygfxrendererd3d11 &&ensured3d11resourcesarecreated ())6     {7id3d11devicecontext* CTX =NULL;8G_d3d11device->getimmediatecontext (&ctx);9 Ten         //update native texture from code One         if(G_texturepointer) A         { -id3d11texture2d* D3dtex = (id3d11texture2d*) G_texturepointer; -D3d11_texture2d_desc DESC;//Save Texture Information theD3dtex->getdesc (&DESC);//Get texture Information -  -             //unsigned char* s_data = new unsigned char[desc. Width*desc. HEIGHT*4]; -             //char* errormessage = new char[80]; +             Charerrormessage[ the]; -  +             if(!S_isinitrender) A             { atS_data = (unsignedChar*)malloc(Desc. Width*desc. height*4); -Initrender (Desc. Width, Desc. Height,2, errormessage);//Initializing render parameters -                 if(NULL = =s_hthread) -                 { -unsigned threadID;//Thread ID -S_hthread = (HANDLE) _beginthreadex (NULL,0, Sfprefreshthread, NULL,0, &threadid);//new thread control rendering frequency in                 } -             } to             //Control Refresh Frequency +             if(S_refreshflag &&S_isinitrender) -             { the                 if( ! Rendertexturewithvideo (Desc. width*4, S_data)) *                 { $Releaserender (2);//Releasing render memoryPanax Notoginseng                      Free(s_data); -S_data =NULL; the                 } +Ctx->updatesubresource (D3dtex,0, NULL, S_data, Desc. width*4,0); AS_refreshflag =false; the             } +         } -Ctx->Release (); $}

In line 1th, we define a global pointer variable, allocate and free memory in the function dorendering, call this function multiple times, and allocate and release the memory of the pointer multiple times.

However, in the VC compiler, the second call to the function will cause an error, but not in the GCC compiler.

So, if you encounter the above situation, change a compiler to try!

Compiler optimization Trap--Global pointer multiple use exception

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.